单击事件不使用Jquery触发按钮

时间:2011-03-20 01:53:00

标签: jquery html

在下面的代码中,单击“保存”按钮不会触发。

任何想法为什么?

[code]

<script type="text/javascript">
$(function () {
    $(".Join").live("click", function () {
        var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
        html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

        $(this).closest("tr").after(html);
        return false;
    });

    $(".Save").live("click", function () {

        var json = { Password: $(this).closest("#pwd").val() };

        $.ajax({
            url: "/Group/ConfirmPassword",
            type: "POST",
            dataType: 'json',
            data: JSON.stringify(json),
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (result.Success == true)
                    window.location.href = result.Url;
                else
                    alert(result.ErrorMessage);
                return false;
            }
        });
    });
});

[/代码]

1 个答案:

答案 0 :(得分:4)

因为当您为$ (".Save")点击创建事件处理程序时,它不存在。使用:

$(function () {
    $(".Join").live("click", function () {
        var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
        html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

        $(this).closest("tr").after(html);

        $(".Save").live("click", function(){
            var json = { Password: $(this).closest("#pwd").val() };

            $.ajax({
                url: "/Group/ConfirmPassword",
                type: "POST",
                dataType: 'json',
                data: JSON.stringify(json),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    if (result.Success == true)
                        window.location.href = result.Url;
                    else
                        alert(result.ErrorMessage);
                    return false;
                }
            });
        });

        return false;
    });
});

修改 基于jsFiddle示例中的发现...将您已加载的代码更改为JoinList:

$("#grouplist").load(
    AppRoot + "Group/JoinList/",
    { page: page_num },
    function(){
        $(".Join").live("click", function () {
            var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
            html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

            $(this).closest("tr").after(html);

            $(".Save").live("click", function(){
                var json = { Password: $(this).closest("#pwd").val() };

                $.ajax({
                    url: "/Group/ConfirmPassword",
                    type: "POST",
                    dataType: 'json',
                    data: JSON.stringify(json),
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        if (result.Success == true)
                            window.location.href = result.Url;
                        else
                            alert(result.ErrorMessage);
                        return false;
                    }
                });
            });

            return false;
        });
    }
);