简体   繁体   English

自动将同一文本框聚焦在模糊处

[英]auto focus the same textbox on blur

I need to focus the text box which on blur moves to next box. 我需要重点放在模糊的文本框上移到下一个框。 but, if an error is detected it needs to be focused again. 但是,如果检测到错误,则需要重新聚焦。

here is my code 这是我的代码

  <input type="text" id="error_code'" name="errorcode[]" onblur="validate_code(this.id)"/>

function validate_code(id){
var error_code=$("#"+id).val();

 if(isNaN(error_code))
    {
         $('#'+id).val("");  
         $('#'+id).focus();
    }
}

however this id not setting focus to same text box. 但是,此ID未将焦点设置为同一文本框。 tnx. tnx。

EDIT: Am able to validate and get result. 编辑:能够验证并获得结果。 But, my question is on re-focusing the text box if error is returned... 但是,我的问题是如果返回错误,请重新调整文本框的位置。

Try something like this.. this works perfectly: 尝试这样的事情。

HTML: HTML:

<input type="text" id="error_code"/>

Jquery blur function: jQuery模糊功能:

$(document).ready(function(){
$("#error_code").blur(function(){
var error_code=$(this).val();
if(isNaN(error_code))     {  
$(this).val("");            
$(this).focus();    
}    
});
});

You need to tryout something like this. 您需要尝试这样的事情。

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <div id="example"> <div> <input type="text" id="error_code" name="errorcode[]" onblur="validate_code(this.id)"/> </div> <script> function validate_code(id){ var error_code=$("#"+id).val(); if(isNaN(error_code)) { setTimeout(function() { $('#'+id).val(""); $('#'+id).focus(); }, 0); } } </script> </div> </body> </html> 

In case of jQuery its already tries to set focus to the field. 在使用jQuery的情况下,它已经尝试将重点放在该字段上。 It will block further focus events to the field. 它将阻止对该领域的进一步关注。 To you need to hack it out by something like this. 对于您,您需要通过类似这样的方式来破解它。

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

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