链接单击时的 JQuery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13731961/
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
JQuery Function on link click
提问by Matthew Colley
I am attempting to call up a jquery function on link click with no success:
我试图在链接点击时调用一个 jquery 函数,但没有成功:
here is my html:
这是我的 html:
<a href="..." id="removeItem" checkID="12" >Delete</a>
<a href="..." id="removeItem" checkID="13" >Delete</a>
<a href="..." id="removeItem" checkID="14" >Delete</a>
$("#removeItem").click(function(checkID) {
return false;
var checkID = $(this).attr("checkID");
$("#removeDialog").dialog( {
buttons: {
"No" : function () {
$(this).dialog("destroy");
$('input#CheckName').focus();
},
"Yes": function () {
$.ajax({
url: "itemRemoveWS.html?id=checkID",
data: {
"action" : "remove",
"id" : checkID
},
success: function (data) {
$("#removeDialog").dialog("destroy");
var ang = '';
var obj = $.parseJSON(data);
$.each(obj, function() {
ang += '<table class="form"><tr><td width="45">' + this["CheckID"] + '</td><td width="140">' + this["Name"] + '</td><td width="95">' + this["CheckNumber"] + '</td><td align="right" width="70">$' + this["Amount"] + '</td><td width="220" style="padding-left: 15px;">' + this["Description"] +'</td><td><a href="#">Delete</a></td></tr></table>';
});
$('#container').html(ang);
$("input#Amount").val('');
$("input#CheckName").val('');
$("input#Check_Number").val('');
$("select#Company").val('MMS');
$("th#dept").hide();
$('input#CheckName').focus();
}
});
}
}
});
});
回答by A. Wolff
You have return false;
as first instruction in your click event callback function. This means you are doing nothing.
您return false;
在点击事件回调函数中有第一条指令。这意味着你什么都不做。
Put it at the very last line of your logic or better change it to e.preventDefault();
using
将它放在逻辑的最后一行,或者更好地将其更改为e.preventDefault();
using
$("#removeItem").click(function(e) {...}
As a side note, $("#removeItem").click(function(checkID) {}
checkID will be an ref to triggered event here, not an element id attribute.
作为旁注,$("#removeItem").click(function(checkID) {}
checkID 将是这里触发事件的引用,而不是元素 id 属性。
And again, ID attribute MUST be unique for each element on each html page.
同样,每个 html 页面上的每个元素的 ID 属性必须是唯一的。
回答by ORION
To call of function on a link click use javascript:void(0) as your href, then add your function call to the onclick event of your link:
要在链接上调用函数,请使用 javascript:void(0) 作为您的 href,然后将您的函数调用添加到链接的 onclick 事件中:
<a runat="server" id="myButton" href="javascript:void(0);" onclick="myFunction();" ></a>
回答by wanovak
Instead of using return false
, do this:
而不是使用return false
,这样做:
checkID.preventDefault();
Also, you're not allowed to have two elements with the same ID.
此外,不允许有两个具有相同 ID 的元素。
回答by Jason
As roasted said, return false is likely your issue. It may be that you meant this:
正如烤所说,返回 false 可能是你的问题。你可能是这个意思:
checkID.preventDefault();