开发者

Click event not firing on button using Jquery

In the code below the Save button click is not firing.

Any ideas why?

[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;
            }
        });
    });
});

[/code]


Because at the time you create the event handler for $(".Save")'s click, it does not exist. Use:

$(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;
    });
});

EDIT: Based on findings in jsFiddle example...change the code you have already for loading in the JoinList to:

$("#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;
        });
    }
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜