使用 href 调用带参数的 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6793159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using a href to call javascript function with parameter
提问by Roobah
I'm trying to call javascript function from a href . the function has a parameter which will be retrieved by the eval function . But some error occurs .
我正在尝试从 href 调用 javascript 函数。该函数有一个参数,该参数将由 eval 函数检索。但是会出现一些错误。
script:
脚本:
function rate(id) {
// do something
}
the a tag that will call the function:
将调用函数的 a 标签:
<a href="javascript:rate(" + <%#Eval("ID")%> + ")" >rate</a>
What am I missing ?
我错过了什么?
回答by JAAulde
You shouldn't be doing it like this, but the issue you're currently up against is probably is your quoting/concatenating.
您不应该这样做,但您目前面临的问题可能是您的引用/连接。
If <%#Eval("ID")%>
simply produces an INT, this should work:
如果<%#Eval("ID")%>
只是产生一个 INT,这应该有效:
<a href="javascript:rate( <%#Eval("ID")%> )" >rate</a>
If it's a string,
如果是字符串,
<a href="javascript:rate( '<%#Eval("ID")%>' )" >rate</a>
should do it for you, although you need to handle the case of <%#Eval("ID")%>
producing anything with a single quote in it.
应该为你做这件事,尽管你需要处理<%#Eval("ID")%>
用单引号生成任何东西的情况。
A Lesson:
一堂课:
I say you shouldn't be doing it like this because the javascript pseudo protocol (javascript:
) is defunct and improper. At worst you should be using an onclick
which returns false. Ideally you'd be assigning the event programatically and preventing the event object's default action.
我说您不应该这样做,因为 javascript 伪协议 ( javascript:
) 已失效且不正确。在最坏的情况下,您应该使用onclick
返回 false 的 。理想情况下,您将以编程方式分配事件并阻止事件对象的默认操作。
回答by pimvdb
You shouldn't use " + + "
, because it will just be replaced with a number:
你不应该使用" + + "
,因为它只会被一个数字替换:
<a href="javascript:rate(<%#Eval("ID")%>)">rate</a>
回答by Stanley Cup Phil
Why call it as an href? You can just make a button and style it like a link. That would be a lot easier
为什么称它为href?您可以制作一个按钮并将其样式设置为链接。那会容易很多
<button id = "myButton" onclick = "rate(<%#Eval("ID")%>)" >rate</button>
#myButton
{
border: none;
padding: none;
background-color: transparent;
}
回答by jfriend00
I don't see any need for the use of eval() at all. Code in quotes like this is evaluated at the top level scope. If you make sure that both rate and ID are defined at that top level scope, then it will work without eval() with this HTML:
我认为根本不需要使用 eval() 。像这样的引号中的代码在顶级范围内进行评估。如果您确保在该顶级范围内定义了 rate 和 ID,那么它将在没有 eval() 的情况下使用此 HTML 工作:
<a href="#" onclick="rate(ID);">rate</a>
and this code:
和这个代码:
var ID = 3;
function rate(id) {
alert(id);
return(false);
}
You can see it work here: http://jsfiddle.net/jfriend00/bqqpM/. The trick to making this work without eval is just making sure that ID and rate are both available at the top level scope.
你可以在这里看到它的工作:http: //jsfiddle.net/jfriend00/bqqpM/。在没有 eval 的情况下完成这项工作的技巧只是确保 ID 和 rate 在顶级范围内都可用。