Javascript alert()取代了之前的代码 [英] Javascript alert() supersedes preceding code
问题描述
我有一个带有提交按钮的联系表.一旦从处理提交数据的服务器端PHP收到响应,我就使用JQuery按照以下顺序执行以下操作:
I have a contact form with a submit button. I am using JQuery to do the following in exactly the following order once a response is received from the server-side PHP that processes the submitted data:
- 将按钮文本更改为发送"
- 在警报中显示收到的响应
代码很简单:
submit_button.text('Send');
alert(response);
但是,无论如何,alert()
总是总是在文本更改之前 触发.这是设计使然吗?有什么技巧可以改变顺序吗?我需要按钮的文本在显示警报之前 进行更改.我什至尝试了以下尝试:
However, the alert()
always seems to trigger before the text change no matter what. Is this by design? Any trick to alter the sequence? I need the button's text to change before the alert is displayed. I even tried the following in vain:
submit_button.text('Send');
if(submit_button.text() == 'Send') { alert(response); }
很抱歉,如果这个问题已经在其他地方得到回答,并要求您将我的方向指向正确的情况.
I'm sorry if this question has already been answered elsewhere and request you to point me in the right direction should that be the case.
推荐答案
您可以将alert
调用包装为setTimeout
调用,这会将alert
调用放在事件循环的末尾,并给出浏览器首先更新DOM的时间.
You can wrap your alert
call into a setTimeout
call, that will put the alert
call at the end of the event loop and will give time for the browser to update the DOM first.
submit_button.text('Send');
setTimeout(function () { alert(response); }, 1);
字段: https://jsfiddle.net/sn9cbqz3/3/
这篇关于Javascript alert()取代了之前的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!