简体   繁体   English

当用户在Twitter Bootstrap 3.1模式对话框中单击时,如何防止单击事件之前的模糊事件!

[英]How to prevent blur event before click event in twitter bootstrap 3.1 modal dialog when user clicks outside of it.!

I am using twitterbootstrap 3.1.0 and i have a modal.I want the modal to be hidden whenever user clicks outside the modal (so i have used data-backdrop="true" and is working fine.) 我正在使用twitterbootstrap 3.1.0,并且我有一个模态。我希望每当用户在模态之外单击时都隐藏该模态(因此我使用了data-backdrop="true"并可以正常工作。)

And my modal dialog has a focused input element having field validation and that input box will be focused whenever modal dialog is shown.On blur of that input box,validation takes place and throws error accordingly.(i want this logic to remain same).!! 我的模态对话框有一个焦点输入元素,该元素具有字段验证,并且每当显示模态对话框时,该输入框都会被聚焦。在该输入框模糊时,将进行验证并相应地引发错误。(我希望这种逻辑保持不变)。 !!

MY PROBLEM is whenever user clicks outside the modal dialog,it disappears showing a error message (field validation for input field).As blur event fired before click , it shows error message and then modal dialog disappears. 我的问题是每当用户在模式对话框外单击时,它就会消失,并显示一条错误消息(输入字段的字段验证)。由于单击前触发了模糊事件,它会显示错误消息,然后模式对话框消失。

I dont want the field validation message to be displayed before hiding modal dialog. 我不希望在显示模式对话框之前显示字段验证消息。

Please HELP ! 请帮忙 ! THANKS A TON IN ADVANCE. 提前致谢。

Blur fires before click and you cannot change that. 单击前会模糊,您无法更改它。 My suggestion would be to move the validation away from blur on to a button click(I am guessing you must be having a Ok or a Submit button). 我的建议是将验证从模糊转移到单击按钮(我猜您必须具有“确定”或“提交”按钮)。 This will resolve your issue. 这样可以解决您的问题。

I also some hacks(I would recommend against it). 我也有一些技巧(我建议反对)。 Since you are having this issue, I assume your modal hides using animation 由于您遇到了这个问题,我假设您使用动画制作了模态皮革

  1. Hide modal without using animation. 隐藏模态而不使用动画。 The message will still be there, but you wont get the time to see it. 该消息仍将在那里,但是您将没有时间看到它。

  2. If you want and know the animation time, you can use a setTimeout after that time to check whether the modal is still visible. 如果您想知道动画时间,可以在该时间之后使用setTimeout来检查模态是否仍然可见。 If yes, show the error message else dont. 如果是,则显示错误消息,否则不显示。

Say modal takes 1 second to disappear, you can try something like this 说模态消失需要1秒,您可以尝试这样的操作

// Inside blur callback function

setTimout(function(){
    if($("#modal").is(":visible")) {
        // show error message
    }
},1100); //1.1 seconds

I will still prefer to move the validation away from blur in your scenario :) 在您的方案中,我还是会更喜欢使验证远离模糊:)

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

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