链接点击时的JQuery功能

时间:2012-12-05 20:36:36

标签: jquery

我试图在链接点击时调用jquery函数但没有成功:

这是我的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();
                    }
                });
            }
        }
    });
});

4 个答案:

答案 0 :(得分:8)

您在点击事件回调函数中有return false;作为第一条指令。这意味着你什么都不做。

将它放在逻辑的最后一行,或者使用

将其更改为e.preventDefault();
$("#removeItem").click(function(e) {...}

作为旁注,$("#removeItem").click(function(checkID) {} checkID将是此处触发事件的引用,而不是元素ID属性。

同样,ID属性必须对每个html页面上的每个元素都是唯一的。

答案 1 :(得分:2)

要在链接上调用函数,请单击使用javascript:void(0)作为您的href,然后将函数调用添加到链接的onclick事件中:

<a runat="server" id="myButton" href="javascript:void(0);" onclick="myFunction();" ></a>

答案 2 :(得分:0)

不要使用return false,而是执行此操作:

checkID.preventDefault();

此外,您不能拥有两个具有相同ID的元素。

答案 3 :(得分:0)

正如烤所说,返回假可能是你的问题。可能你的意思是:

checkID.preventDefault();
相关问题