简体   繁体   English

OnClick事件绕过了onBlur事件

[英]OnClick event is bypassing onBlur event

My code relies on onBlur to validate the userid, however if the user just types in the user and then directly click on submit button, the onBlur event is not firing. 我的代码依靠onBlur来验证用户ID,但是如果用户只是键入用户,然后直接单击Submit按钮,则不会触发onBlur事件。

<td><input type='text' name="USERID" onBlur="doValidateUserID(this)"></td>
<td><input type="button" name="assetSaveButton"  value="Save" onClick="doSomeStuff(this)"></td>

Below is pseudo ajax call 下面是伪ajax调用

function doValidateUserID(userID) { 
// ajax call goes here
}

My problem is that doValidateUserID is not even being called after user types in the value and clicks on submit button. 我的问题是,在用户键入值并单击提交按钮后,甚至都没有调用doValidateUserID。 I tried with onChange instead of onBlur but in vain. 我尝试使用onChange而不是onBlur但徒劳。

EDIT: Apparently adding alerts have changed the behaviour of events, now on clicking the save button , doValidateUserID is called but doSomeStuff is skipped altogether. 编辑:显然,添加警报已更改事件的行为,现在单击“保存”按钮,将调用doValidateUserID,但完全跳过了doSomeStuff。 I have to do another click to call doSomeStuff 我必须再次单击以调用doSomeStuff

The onblur will always be called before click, though since you do an ajax call inside of the handler the ajax result call will always be run after the click event since it is non blocking (unless you set it to be sync which I would advise against) onblur总是在单击之前被调用,尽管由于您在处理程序中进行了ajax调用,所以ajax结果调用将始终在click事件之后运行,因为它是非阻塞的(除非您将其设置为sync,我建议不要这样做) )

I would recommend blocking the submit button until the ajax has finished or deferring the submit until the verification completed 我建议在ajax完成之前阻止提交按钮,或者在验证完成之前推迟提交

Hope this helps :) 希望这可以帮助 :)

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

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