我有两个按钮,其中一个已经与来自不同站点的 ajax 调用绑定(bind)。我需要使用第一个按钮调用 ajax,并且还需要在 ajax 完成后将一些数据附加到结果中。
I have no access to edit the ajax function or existing jQuery. All I can do is that create a new function. So please don't ask me to edit ajax jQuery or function.
我只需从控制台调用它即可。是否有任何 jQuery 函数可以知道 ajax 是否被调用以及是否有成功响应。
我尝试使用 ajaxStart()和 ajaxStop() 。但不起作用。
这是我尝试过的。
jQuery('.a').click(function(){
var valuetopass;
//Copy Value from input of A
jQuery(document).ajaxStart(function(){
console.log('Click Ajax Button');
valuetopass = jQuery('.a_value').val();
});
//Call Ajax
jQuery('.b').click();
//Paste Value to the result appended
jQuery(document).ajaxStop(function(){
console.log('Appended the result input field(b_value) to the html.');
//Paste the value in new field from ajax.
jQuery('.b_value').val(valuetopass);
});
});
<input class="a_value" value="ABC"/>
<button class="a">Button to initiate ajax</button>
<button class="b">Button having Ajax</button>
当我们点击按钮 A
时它将启动点击按钮 B
ajax 会调用。
之后,结果将在 HTML 中附加一个输入字段,如 <input class="b_value"/>
。我需要使用外部 jQuery 函数向该字段添加值。
我也尝试过
setTimeout(function(){
jQuery('.b_value').val(valuetopass)
},500);
但我们不能说 500 毫秒就是所花费的确切时间。是否有可能找到调用并完成的ajax。
最佳答案
你可以使用.ajaxComplete(),像这样使用
$( document ).ajaxComplete(function(event, xhr, settings) {
console.log(xhr.responseText);
});
这里 xhr.responseText 将获得 jQuery ajax 方法发出的每个 ajax 调用的响应。
所以你的回应<input class="b_value"/>
可以在这个回调中处理
关于javascript - jQuery 使用 jQuery 单击按钮并在 ajax 成功后将数据附加到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56596014/