简体   繁体   English

在href标签中调用参数时,如何将其传递给函数

[英]How can I pass an argument to a function when I have called it in the href tag

I am creating a apraisal web application , Please help me solve this logic/syntax issue : I want to create a table with the cell content as hyperlink , so that when the user clicks on the link it will open a new window to accept data , also it should display the content of the cell clicked on the next page. 我正在创建一个通用的Web应用程序,请帮助我解决此逻辑/语法问题:我想创建一个表格,其中单元格内容为超链接,以便当用户单击链接时,它将打开一个新窗口以接受数据,还应该显示在下一页上单击的单元格的内容。

Here is my Javascript code: 这是我的Javascript代码:

row = table.insertRow(-1);

var cell = row.insertCell(-1);

cell.innerHTML = '<a href="#" onclick = func(name);>'+name+'</a>';


function func(name)
        {


            localStorage.setItem("Selectedname",name);

            window.location.href="form_apraise.html";

        }

Thank you 谢谢

尝试使用enable pop window.open(' https://stackoverflow.com/','_blank ');

cell.innerHTML = '<a onclick="func("' + name + '");">' + name + '</a>';

This should work. 这应该工作。 You don't really need the href attribute as that's not very useful in this case. 您实际上并不需要href属性,因为在这种情况下这不是很有用。 Any element responds to an onclick event. 任何元素都响应一个onclick事件。

You could also put the parameters in additional attributes: 您还可以将参数放在其他属性中:

cell.innerHTML = '<a data-name="' + name + '" onclick="func();">' + name + '</a>';

Then in your func() you use this.dataset.name More on this here 然后在你的FUNC()使用this.dataset.name 这一这里更多

Use 'this' as a reference to the cell on which click event is fired. 使用“ this”作为对触发点击事件的单元格的引用。

  cell.innerHTML = '<a href="#" onclick = "func(this);">'+name+'</a>';

Access the cell value using the element.text 使用element.text访问单元格值

   function func(elem)
    {
        var name =elem.text;

        localStorage.setItem("Selectedname",name);

        window.location.href="form_apraise.html";

    }

You have a lot of options here, but I will tell you 3 of them: 您在这里有很多选择,但我会告诉您其中3个:

1- Normally you could just send the parameter in your URL and that will be handle by your backend, like this: http://www.example.com/viewName/myName an then you could use window.open(URL, '_blank') that will open a new window 1-通常,您只需在URL中发送参数即可,该参数将由您的后端处理,例如: http : //www.example.com/viewName/myName ,然后您可以使用window.open(URL,'_blank '),这将打开一个新窗口

2- You could use a Popup window that get the information with AJAX and show the popup window to accept whatever you want to do. 2-您可以使用弹出窗口通过AJAX获取信息,并显示弹出窗口以接受您想做的任何事情。

3- You could create your cell with data-name attribute, assign one class to your cell and with any library or by Javascript only assign an event to all the elements with that class, read the attribute and call window.open(URL, '_blank') something like this: 3-您可以使用data-name属性创建单元格,为单元格和任何库分配一个类,或者通过Javascript仅将事件分配给该类的所有元素,读取属性并调用window.open(URL,' _blank')如下所示:

<tr><td class="myClass" data-name="Your Name">Your Name</td></tr>

In javascript(jQuery): 在javascript(jQuery)中:

$(document).ready(function(){
   $(".myClass").click(function(){
     window.open("http://example.com/"+$(this).data("name"),"_blank")
   })
})

In your case the easy way is just pass the name as parameter, open a new window and then show the client the information about that name. 在您的情况下,简单的方法是将名称作为参数传递,打开一个新窗口,然后向客户端显示有关该名称的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM