【问题标题】:jQuery ajax post fired twicejQuery ajax post 触发了两次
【发布时间】:2011-10-01 16:44:51
【问题描述】:

我正在使用 jquery 对页面执行 $.post,问题是该帖子在触发时执行了两次,我尝试在 IE 和 firefox 中运行该页面,结果是相同的,并且使用firebug 显示请求 bieng 提交了两次。

我需要你的帮助,在此先感谢。 代码如下:

jQuery().ready(function() {
    //Reply buttons binding live event
    $(".ticketButton").live('click', function(e) {
        e.preventDefault(); 

        //build dialog with id & title
        $( "#msgs-form:ui-dialog" ).dialog( "destroy" );
        var ticket_ID= $(this).attr("title");
        var ticket_title= $(this).attr("msgTitle");
        $("#msgs-dialog").attr("title", ticket_title);

        //fetch data from the server to fill dialog content
        $.post("server_processing_tickets_response.php", {'getTickets' : ticket_ID},
                function(data){
                var div_data;
                //build div's of classes .ticket-1 and ticket-0 from the returned json object
                $.each(data.tickets_data, function(i, tickets_data)
                    {
                    div_data += "<div class='ticket-0'><span>" + tickets_data.content + "</span></div>";
                    });
                $(div_data).appendTo("#ticketsWrap");
                }, "json");

        $("#msgs-form #ticketID").attr("value", ticket_ID);
                $( "#msgs-dialog" ).dialog({
                    autoOpen: false,
                    title: ticket_title,
                    height: 'auto',
                    width: 460,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                    close: resetDialog()
                });

        $( "#msgs-dialog" ).dialog( "open" );
    });

    function resetDialog(){
    $("#ticketsWrap").empty();
    }

}

感谢您的快速回复! 这是我正在使用的完整 javascript 代码:

$(document).ajaxStart(function(){
$('#real-content').showLoading();
    }).ajaxStop(function(){
$('#real-content').hideLoading();
    });

//ajax forms submission
$(function() {      

        var msgs_table = $('#msgs_table').dataTable( {
                "bJQueryUI": true,
                "bProcessing": false,
                "bServerSide": true,
                "sAjaxSource": "server_processing_msgs.php",
                "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "aoColumns": [ 
                                    { "sWidth": "30px" },
                                    { "sWidth": "100px" },
                                    { "sWidth": "200px" },
                                    { "sWidth": "250px" },
                                    { "sWidth": "50px" }
                            ],
                "oLanguage": {
                                "sProcessing": "...تحميل",
                                "sZeroRecords": "لايوجد لديك تذاكر حالياً",
                                "sInfoEmpty": "",
                                "sInfoFiltered": "",
                                "sInfo": "يتم إظهار _START_ إلى _END_ من _TOTAL_ تذكرة",
                                "oPaginate": {
                                                "sFirst":    "البداية",
                                                "sPrevious": "السابق",
                                                "sNext":     "التالي",
                                                "sLast":     "النهاية"
                                              },
                                "sSearch": "بحث ضمن التذاكر:",
                                "sLengthMenu": "عرض _MENU_ تذكرة"

                             },
                "aoColumnDefs": [{
                        "fnRender": function ( oObj ) {
                                        return "<input type='button' style='padding:3px 6px;'  name='ticket-ID-button' value='عرض' msgTitle='" + oObj.aData[3] + "\' title='" + oObj.aData[0] + "\' class=\"ticketButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\" />";  
                                    },
                            "aTargets": [ 0 ]}]
            } );



    jQuery().ready(function() {

                $( "#ٍSucsessMessage" ).dialog({
                    autoOpen: false,
                    height: 'auto',
                    width: 300,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                });
                $( "#ٍFailMessage" ).dialog({
                    autoOpen: false,
                    height: 'auto',
                    width: 300,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                });


    //Reply buttons binding live event
    $(".ticketButton").live('click', function(e) {
        e.preventDefault(); 

        //build dialog with id & title
        $( "#msgs-form:ui-dialog" ).dialog( "destroy" );
        var ticket_ID= $(this).attr("title");
        var ticket_title= $(this).attr("msgTitle");
        $("#msgs-dialog").attr("title", ticket_title);

        //fetch data from the server to fill dialog content
        $.post("server_processing_tickets_response.php", {'getTickets' : ticket_ID},
                function(data){
                var div_data;
                //build div's of classes .ticket-1 and ticket-0 from the returned json object
                $.each(data.tickets_data, function(i, tickets_data)
                    {
                    div_data += "<div class='ticket-0'><span>" + tickets_data.content + "</span></div>";
                    });
                $(div_data).appendTo("#ticketsWrap");
                }, "json");

        $("#msgs-form #ticketID").attr("value", ticket_ID);
                $( "#msgs-dialog" ).dialog({
                    autoOpen: false,
                    title: ticket_title,
                    height: 'auto',
                    width: 460,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                    close: resetDialog()
                });

        $( "#msgs-dialog" ).dialog( "open" );
    });

    function resetDialog(){
    $("#ticketsWrap").empty();
    }

    $( "#msgs-reply-button" ).click(function() {
        $("#msgs-form").submit();
        $("#msgs-dialog").dialog( "close" );
    }); 

    //reply form ajax submit
    $("#msgs-form").submit(function(e){
        e.preventDefault(); 
        $.post("server_processing_tickets_response.php", $("#msgs-form").serialize(),
        function(data){
            if(data.valid == 'valid'){ 
                $( "#ٍSucsessMessage" ).dialog( "open" );
                setTimeout(function(){$( "#ٍSucsessMessage" ).dialog("close")},1500);
            }
            else if(data.valid == 'invalid')
            {
                $( "#FailMessagee" ).dialog( "open" );
                setTimeout(function(){$( "#FailMessagee" ).dialog("close")},3000);
            }
        }, "json");

    });
    //Reply buttons in table//

它与请求标头有关吗? 这是我提交表单后得到表单萤火虫的标题: 响应标头 日期 2011 年 7 月 9 日星期六 13:29:51 GMT 服务器 Apache/2.2.17 (Win32) PHP/5.3.5 X-Powered-By PHP/5.3.5 到期时间 1981 年 11 月 19 日星期四 08:52:00 GMT Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 杂注无缓存 内容长度 24 保活超时=5,最大值=84 连接保活 内容类型 text/html 请求标头 主机 127.0.0.1:8888 用户代理 Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 接受 application/json, text/javascript, /; q=0.01 Accept-Language en-us,en;q=0.5 接受编码 gzip,放气 接受字符集 ISO-8859-1,utf-8;q=0.7,*;q=0.7 保活 115 连接保持活动 内容类型应用程序/x-www-form-urlencoded;字符集=UTF-8 X-Requested-With XMLHttpRequest 推荐人http://127.0.0.1:8888/Lightc-tables/msgs.php 内容长度 41 Cookie PHPSESSID=ku8e6o3h2bl27ltofkir58a626

并且具有不同Expires的相同标头也在此标头之后重复..

【问题讨论】:

  • 尝试在 firebug 或 chrome 中调试你的代码
  • Bakk 这个问题解决了吗?如果是怎么办?

标签: jquery browser


【解决方案1】:

你需要使用一些小触发器;)

$(".ticketbutton").unbind("click");
$(".ticketbutton").unbind("live");
$(".ticketbutton").die();

【讨论】:

  • 所以根据业务逻辑,有时它不适用于系统。它作为一个函数工作,但并没有真正帮助我,因为我有一个非常复杂的系统!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多