简体   繁体   English

jQuery Ajax发布数据两次

[英]Jquery Ajax Posting Data Twice

I have the following jquery script that is supposed to post data to the data base, 我有以下应该将数据发布到数据库的jquery脚本,

//delegated submit handlers for the forms inside the table
                                    $('#issue').on('click', function (e) {
                                        e.preventDefault();

                                        //read the form data ans submit it to someurl
                                        $.post('<?php echo base_url() ?>pharm_profile/dept_issue/', $('#Issues_Form').serialize(), function () {
                                            //success do something
                                            alert("Success Approved Successfully");
                                            var url = "<?php echo base_url() ?>pharm_profile";    
                                            $(location).attr('href',url);
                                        }).fail(function () {
                                            //error do something
                                            alert("Failed please try again later or contact the system administrator");
                                        })
                                    })

Whenever I click the button, the script runs twice / posts the data twice. 每当我单击按钮时,脚本就会运行两次/将数据发布两次。 How can I control that to only once? 我怎么只能控制一次?

This can happen if your DOM has more than one element "#issue" inside. 如果您的DOM内部包含多个元素“ #issue”,则可能会发生这种情况。 This can happen, even if only one is shown, On the other hand, the "on" actually gets registered every time you add an element with id "issue". 即使只显示一个,也会发生这种情况。另一方面,每次添加ID为“ issue”的元素时,“ on”实际上都会被注册。 So if you on happen to add this elements dynamically, each time you add it, a new click event handler gets assigned... 因此,如果您碰巧动态添加了此元素,则每次添加它时,都会分配一个新的click事件处理程序...

To be pragmatic, because it doesn't seam that you really wanted that behavior... before the "on" registration use the off.. 务实的是,因为您不会真正希望自己的行为……在“打开”注册之前,请使用“关闭”。

$('#issue').off('click');
$('#issue').on('click',function....);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM