I have a bunch of elements that act as buttons for users to click on. In certain circumstances, I want to force click the buttons myself. I have added click event handlers to all these elements and they work fine.
我有一堆元素作为按钮供用户点击。在某些情况下,我想强制点击按钮。我已经为所有这些元素添加了单击事件处理程序,它们工作正常。
The problem is when I want to force click two of the buttons in succession like:
问题是当我想强制点击两个按钮时,如:
jQuery('#spProfileTab-some-button').click();
jQuery('#spProfileMenu-another-button').click();
this doesnt work however, because the click handler for the second one isnt bound until the ajax for the first click handler completes.
但是,这不起作用,因为第二个处理程序的单击处理程序没有绑定,直到第一个单击处理程序的ajax完成。
Basically need to chain them or have the second wait for first to complete, but havent found the right syntax yet. any help would be appreciated.
基本上需要链接它们或让第二个等待首先完成,但还没有找到正确的语法。任何帮助,将不胜感激。
5 个解决方案
#1
0
You can use jquery trigger ,
你可以使用jquery触发器,
please see the documentation here
请参阅此处的文档
http://api.jquery.com/trigger/
http://api.jquery.com/trigger/
#2
0
You need to call the second click from the callback of the ajax. So if you are using load()for example you would be
您需要从ajax的回调中调用第二次单击。因此,如果您正在使用load(),那么您将是
$('selector').load('content',function(){$(otherselecter).click()});
You dont have a chain/queue there because the semicolon really makes that 2 separate commands. So just keep that in mind.
你没有链/队列,因为分号确实产生了2个单独的命令。所以请记住这一点。
#3
0
So the sequence of events you want is 1) some-button is clicked, 2) the AJAX in the click handler some-button binds a click handler to another-button, 3) that click handler is called?
所以你想要的事件序列是1)点击某个按钮,2)点击处理程序中的AJAX按钮将点击处理程序绑定到另一个按钮,3)点击处理程序被调用?
In that case you need to trigger 3) at the end of the AJAX success handler for 2, i.e.
在这种情况下,您需要在AJAX成功处理程序结束时触发3),即
$('some-button'.click( function() {
$.ajax( {... success: function() {
...
$('another-button').click(another-button-handler);
$('another-button').click();
}
});
Because the AJAX call is asynchronous that's how you guarantee that the click() call to another-button is handled after the AJAX completes and the new click handler is bound.
因为AJAX调用是异步的,所以你可以保证在AJAX完成并且绑定新的点击处理程序之后处理对另一个按钮的click()调用。
#4
0
Unless I've misunderstood your question, by the sound of it the click handler on the first element is calling an AJAX function (perhaps jQuery's get
, post
or load
)?
除非我误解了你的问题,否则第一个元素上的click处理程序会调用一个AJAX函数(也许是jQuery的get,post或load)?
If that's the case, you should be able to simply trigger the click on your second element in the callback function, for example something like this:
如果是这种情况,您应该能够简单地触发回调函数中第二个元素的单击,例如:
$.post("somepage.php", function(data) {
//Successful AJAX call
$('#spProfileMenu-another-button').click();
});
I'm assuming your the click event on the first element is something like this:
我假设您在第一个元素上的click事件是这样的:
$('#spProfileTab-some-button').click(function() {
//AJAX call as above
});
#5
0
I'd probably use the deferred.done() method as explained here: http://api.jquery.com/deferred.done/
我可能会使用deferred.done()方法,如下所述:http://api.jquery.com/deferred.done/
In your "force click" example, you'd probably call the .done() method where you've created your AJAX call.
在“强制单击”示例中,您可能会调用.done()方法,您已在其中创建了AJAX调用。
So like...
所以......
$.get("ajax.php").done(function() {
$('another-button').click();
});
#1
0
You can use jquery trigger ,
你可以使用jquery触发器,
please see the documentation here
请参阅此处的文档
http://api.jquery.com/trigger/
http://api.jquery.com/trigger/
#2
0
You need to call the second click from the callback of the ajax. So if you are using load()for example you would be
您需要从ajax的回调中调用第二次单击。因此,如果您正在使用load(),那么您将是
$('selector').load('content',function(){$(otherselecter).click()});
You dont have a chain/queue there because the semicolon really makes that 2 separate commands. So just keep that in mind.
你没有链/队列,因为分号确实产生了2个单独的命令。所以请记住这一点。
#3
0
So the sequence of events you want is 1) some-button is clicked, 2) the AJAX in the click handler some-button binds a click handler to another-button, 3) that click handler is called?
所以你想要的事件序列是1)点击某个按钮,2)点击处理程序中的AJAX按钮将点击处理程序绑定到另一个按钮,3)点击处理程序被调用?
In that case you need to trigger 3) at the end of the AJAX success handler for 2, i.e.
在这种情况下,您需要在AJAX成功处理程序结束时触发3),即
$('some-button'.click( function() {
$.ajax( {... success: function() {
...
$('another-button').click(another-button-handler);
$('another-button').click();
}
});
Because the AJAX call is asynchronous that's how you guarantee that the click() call to another-button is handled after the AJAX completes and the new click handler is bound.
因为AJAX调用是异步的,所以你可以保证在AJAX完成并且绑定新的点击处理程序之后处理对另一个按钮的click()调用。
#4
0
Unless I've misunderstood your question, by the sound of it the click handler on the first element is calling an AJAX function (perhaps jQuery's get
, post
or load
)?
除非我误解了你的问题,否则第一个元素上的click处理程序会调用一个AJAX函数(也许是jQuery的get,post或load)?
If that's the case, you should be able to simply trigger the click on your second element in the callback function, for example something like this:
如果是这种情况,您应该能够简单地触发回调函数中第二个元素的单击,例如:
$.post("somepage.php", function(data) {
//Successful AJAX call
$('#spProfileMenu-another-button').click();
});
I'm assuming your the click event on the first element is something like this:
我假设您在第一个元素上的click事件是这样的:
$('#spProfileTab-some-button').click(function() {
//AJAX call as above
});
#5
0
I'd probably use the deferred.done() method as explained here: http://api.jquery.com/deferred.done/
我可能会使用deferred.done()方法,如下所述:http://api.jquery.com/deferred.done/
In your "force click" example, you'd probably call the .done() method where you've created your AJAX call.
在“强制单击”示例中,您可能会调用.done()方法,您已在其中创建了AJAX调用。
So like...
所以......
$.get("ajax.php").done(function() {
$('another-button').click();
});