简体   繁体   English

输入文本框关注模糊事件

[英]Input text box focus on blur event

Actually I'm trying to set focus on text-box when focus is lost.The reason behind this is that there is requirement to forcefully set focus on text-box if some validation fails(like empty field validation) and validation is perform on blur event of that text box. 实际上,我正在尝试在焦点丢失时将焦点设置在文本框上,其背后的原因是,如果某些验证失败(例如空字段验证)并且验证在模糊时执行,则需要强制将焦点设置在文本框上该文本框的事件。 I have tried it on fiddle also but it seems like focus is there but cursor is not blinking. 我也在小提琴上尝试过,但好像有焦点但光标没有闪烁。

Please refer link : http://jsfiddle.net/zHeJY/ Please refer link : http Please refer link : //jsfiddle.net/zHeJY/

Please let me know the reason behind this and solution for same. 请让我知道其背后的原因和解决方案。

Thanks in advance. 提前致谢。

First : This is a bad idea to trap user inside an input unless validation passes! 首先 :除非验证通过,否则将用户困在输入中是个坏主意! Users should be allowed to focus whatever they want. 应该允许用户专注于他们想要的任何东西。 Ideally, you can prevent a form submission if validation fails. 理想情况下,如果验证失败,则可以阻止表单提交。

Problem : (1) You don't have any other element in the fiddle you provided. 问题 :(1)您提供的小提琴中没有任何其他元素。 (2) You are not validating anything, just doing an endless loop for blur-focus cycle! (2)您没有进行任何验证,只是对模糊焦点循环进行了无限循环!

Solution : 解决方案

See this fiddle: http://jsfiddle.net/zHeJY/7/ 看到这个小提琴: http : //jsfiddle.net/zHeJY/7/

With a proper validation routine in place (and another element available), it will work. 有了适当的验证例程(以及其他可用元素),它将起作用。

HTML : HTML

<input id="setFocus"/>
<input id="other" />

JS : JS

$("#setFocus").on("blur", function(e) {
    if (! validate(this)) {
        $(this).focus();
    }
});

try this 尝试这个
HTML 的HTML

<div id="something">
<input type="text" id="setFocus"/>
<input type="text" id="setFocus1"/>
</div>

JS JS

$("#something").mouseup("blur",function(){
    $(this).children(":first").focus();
})

you should have parent element for this on click of any where you will get the required result 单击任何可获得所需结果的位置时,都应具有父元素

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js">
</script>
<script type="text/javascript">
$( document ).ready(function() {
$(document).on('blur', "#setFocus", function (e) {
$(this).focus();
});
});
</script>
</head>
<body>
<input type="text" id="setFocus"/>
<input type="text" id="set"/>
</body>
</html>

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

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