0

我有 jquery 代码来使用 ajax 发送请求,但是在成功后功能按键不再触发这是我的代码

    $(".id_sort").bind('keypress',function(e){
    if (e.which == 13){
        var index_fix = [];
        var index_ori = [];
        for (i=0;i < $("tbody tr").length; i++){
            index_ori.push(i);
        }
        $(".id_sort").each(function(){
            index_fix.push(Number($(this).val())-1); 
        });
        if (JSON.stringify(index_fix) !== JSON.stringify(index_ori)){
            data = { key : 'sort', index : JSON.stringify(index_fix)};
            $.ajax({
                url : "/ajax/",
                data : data,
                type : "POST",
                cache : false,
                success : function (resp){
                        $(".data").html(resp);
                        // what should i do here..keypress enter doesn't work in second    time
                } 
            });     
        }
        else {
            alert("data sama coy");
        }
    }
});
4

2 回答 2

0

检查您的浏览器控制台是否有 JavaScript 错误。我怀疑可能是 JavaScript 在第一次按键后抛出错误并且不再运行此代码。

如果 .id_sort 类的任何字段中除了空白之外还有其他任何内容,那么当您转换为数字时您会得到 NaN 并且当您将值传递给服务器时,它是否会接受 NaN 作为值?

于 2013-02-16T07:08:03.743 回答
0

在我看来,您绑定的 dom 节点可能会被您的$(".data").html(resp);调用替换/覆盖。绑定到事件仅适用于已经在 dom 中的节点。

因此,如果是这种情况,那么您要么需要在替换 dom 后在成功回调中重新绑定,要么可以将 click 事件绑定到更高的节点。例如:$(".data").on("keypress", ".id_sort", function() { ... });

于 2013-02-16T07:30:44.303 回答