简体   繁体   English

Jquery .ajax beforeSend似乎从按钮中删除了click事件处理程序?

[英]Jquery .ajax beforeSend seems to remove the click event handler from a button?

I'm trying to work with wordpress to stop an ajax request sent by Jquery and re-submit it with some additional data. 我正在尝试使用wordpress来停止Jquery发送的ajax请求,并使用一些额外的数据重新提交它。

Here's how its working: 以下是它的工作原理:

  • User clicks a submit button. 用户单击提交按钮。

  • Wordpress submits the ajax request Wordpress提交ajax请求

  • I have a global beforeSend function set up using .ajaxSetup in this way: 我有一个全球beforeSend功能设置使用.ajaxSetup这样:

    $.ajaxSetup( {beforeSend: monitorRequests } );

  • When wordpress submits the request, its intercepted by my function, which calls another function that submits another ajax request, while monitorRequests ' returns false, which cancels the original ajax request. 当wordpress提交请求时,它被我的函数拦截,该函数调用另一个提交另一个ajax请求的函数,而monitorRequests '则返回false,这会取消原始的ajax请求。

  • My own ajax request is sent, without any problems. 我发送了自己的ajax请求,没有任何问题。

However here's the issue. 不过这是问题所在。 After doing this once, if the user clicks the submit button again, wordpress's click handler is not called. 执行此操作一次后,如果用户再次单击提交按钮,则不会调用wordpress的单击处理程序。 I'm wondering why this is, whether this has anything to do with the ajax request being cancelled? 我想知道为什么会这样,这是否与取消ajax请求有关? Or anything else? 还是其他什么? What should I do to make it so that the click handler continues to be called? 我该怎么做才能继续调用点击处理程序?

想出来之后,Wordpress在ajax请求期间禁用了按钮,并且由于从未调用过wordpress的回调,它仍然被禁用。

$.ajaxSetup globally affects ajax calls in your page. $ .ajaxSetup全局影响页面中的ajax调用。 You have no idea how many AJAX implemented in WP plugins and How each its custom implement. 您不知道WP插件中实现了多少AJAX以及它们的自定义实现方式。

Try narrow your scope to specific element. 尝试将范围缩小到特定元素。 You may uses 你可以使用

$('.submit').bind('click', function(){ /* your fn */ }); 
//You can have many 'click' binding eg..
$('.submit').bind('click', function(){ /* another */ }); 
$('.submit').bind('click', function(){ /* yet another */ }); 

insert your script before existing one. 在现有脚本之前插入脚本。 You function will be called first. 您将首先调用您的函数。

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

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