4

我有一个在我的页面上连续运行的脚本,它会检测是否有新项目已提交到我的数据库,此时它会向我的页面添加额外的 html:

var count_cases = -1;

setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "new_lead_alerts_process.php",
        dataType: 'json',
        cache: false,
        success : function(response){
            $.getJSON("new_lead_alerts_process.php", function(data) {
                if (count_cases != -1 && count_cases != data.count) {
                    //add new HTML element to page
                    $('#content').prepend("<div class=\"reportRow\"><div id=\"reportRowLeft\" style=\"width: 63px;\">"+dayOfMonth+"</div><div id=\"reportRowCenter\">"+timeSubmitted+"</div><div id=\"reportRowRight\" style=\"width: 126px;\"><div id=\"claimButton"+data.id+"\"><form action=\"\" method=\"post\" name=\""+data.id+"\"><input type=\"hidden\" id=\"lead_id"+data.id+"\" name=\"lead_id\" value=\""+data.id+"\" /><input type=\"hidden\" id=\"client_id"+data.id+"\" name=\"client_id\" value=\""+data.client_id+"\" /><input type=\"hidden\" id=\"user_id"+data.id+"\" name=\"user_id\" value=\"1\" /><input type=\"image\" name=\"submit\" class=\"claimButton\" onClick=\"expand('claimed"+data.id+"');collapse('claimButton"+data.id+"');\" src=\"images/claim.gif\" id=\""+data.id+"\" style=\"width: 126px; height: 29px; margin: 0; border: 0px; padding: 0; background: none;\"></form></div><img id=\"claimed"+data.id+"\" style=\"display: none;\" src=\"images/claimed.gif\" /></div><div id=\"clear\"></div></div>");
                }
            count_cases = data.count;
            });
        }
    });
},1000);

当用户单击新创建的元素中的按钮时,我有另一个脚本通过 AJAX 向数据库提交更新。它看起来像这样:

//update db when lead is claimed
$(document).ready(function(){
    $(".claimButton").click(function(){
        var element = $(this);
        var Id = element.attr("id");
        var client_id = $("#client_id"+Id).val();
        var user_id = $("#user_id"+Id).val();
        var lead_id = $("#lead_id"+Id).val();
        var dataString = 'client_id='+client_id+'&user_id='+user_id+'&lead_id='+lead_id;

        $.ajax({
            type: "POST",
            url: "new_lead_alerts_update.php",
            data: dataString,
            cache: false
        });
    return false;});
    });

我的问题是,第二个 AJAX 提交似乎不起作用。我认为这是因为 $(document).ready 命令仅在页面首次加载时触发,并且由于我通过 AJAX 动态更新页面内容,添加到页面的任何新元素都可以'不提交。

我尝试过 .delegate 和 .live,而不是 .ready,但似乎都不起作用。

任何帮助将不胜感激。

4

2 回答 2

3

将 document.ready 中的功能设置为回调,然后每次从 ajax 将新元素附加到页面时执行此操作,如下所示:

$(document).ready(function(){ MakeClaim(); });

function MakeClaim(){
 $(".claimButton").click(function(){
    var element = $(this);
    var Id = element.attr("id");
    var client_id = $("#client_id"+Id).val();
    var user_id = $("#user_id"+Id).val();
    var lead_id = $("#lead_id"+Id).val();
    var dataString = 'client_id='+client_id+'&user_id='+user_id+'&lead_id='+lead_id;

    $.ajax({
        type: "POST",
        url: "new_lead_alerts_update.php",
        data: dataString,
        cache: false
    });
 return false;});
}

success : function(response){
        $.getJSON("new_lead_alerts_process.php", function(data) {
            if (count_cases != -1 && count_cases != data.count) {
             //that huge string
             MakeClaim();
            }
            count_cases = data.count;
        });
}

编辑

尝试改变这个:

onClick=\"expand('claimed"+data.id+"');collapse('claimButton"+data.id+"');\"

onClick=\"expand('claimed"+data.id+"');collapse('claimButton"+data.id+"');return false;\"
于 2012-10-05T19:19:24.370 回答
0

请试试这个:

function TryIt(getId)
{
        var element = $(getId);
        var Id = element.attr("id");
        var client_id = $("#client_id"+Id).val();
        var user_id = $("#user_id"+Id).val();
        var lead_id = $("#lead_id"+Id).val();
        var dataString = 'client_id='+client_id+'&user_id='+user_id+'&lead_id='+lead_id;

        $.ajax({
            type: "POST",
            url: "new_lead_alerts_update.php",
            data: dataString,
            cache: false
        });

}

只需在 claimButton 中使用 javascript onclick 函数并随时调用 TryIt ()函数。

于 2012-10-05T19:28:04.283 回答