I've got problems with setting focus to an input field after validating it.

在验证后将焦点设置到输入字段时遇到问题。

I've got 2 fields: from and to.

我有两个领域:从和到。

   <spring:message code="template.fields.label.from"/>: 
       <mvc:input  path="templateFields.selectorRangeFrom" 
       onchange="validateNumber('templateFields.selectorRangeFrom',true)"/>
   <spring:message code="template.fields.label.to"/>: 
      <mvc:input path="templateFields.selectorRangeTo"   
     onchange="validateNumber('templateFields.selectorRangeTo',true)"/>

And I've got method validateNumber() which validates the field and returns false if the field is invalid, true otherwise. However the focus never stays on invalid number, it will always go the next object.

我有方法validateNumber()验证字段,如果字段无效则返回false,否则返回true。然而,焦点永远不会停留在无效数字上,它将永远是下一个对象。

function validateNumber(index,isInteger) {
 var object = document.getElementById(index);
 var value = object.value;   
 if (testNumeric2(value,isInteger)==false)   {
     alert('Please correct the value: ' + value);               
     object.focus();
     object.select();
     return false;  
 } 
return true;    
 }    

I have found out that if I add: event.returnValue=false (before returning false), then it works for IE. But I can't find the solution for Firefox.

我发现如果我添加:event.returnValue = false(在返回false之前),那么它适用于IE。但我找不到Firefox的解决方案。

Thanks for any suggestions, matali

感谢您的任何建议,matali

2 个解决方案

#1


setTimeout() allows you to defer the execution of a function for a number of milliseconds - if you use zero, this simply means, "do this as soon as you're done with whatever you're doing now" - handling the event, in your case.

setTimeout()允许你将函数的执行推迟几毫秒 - 如果你使用零,这只是意味着,“一旦你完成了你正在做的任何事情,就这样做” - 处理事件,在你的情况下。

Try this:

if (! /** check field **/) {
   /** show error **/
   setTimeout((function() { object.focus() }), 0);
}

Basically you would be creating a function that will focus your input box, and then instructing the browser to call it as soon as it's done doing the default handling of the event.

基本上你会创建一个函数来聚焦输入框,然后指示浏览器在完成事件的默认处理后立即调用它。

更多相关文章

  1. 在Javascript中将下拉字段乘以文本字段
  2. 【问题解决方案】ImportError: No module named 'pygal'
  3. Python 黏包及黏包解决方案
  4. 如何在Python Django中附加数组字段
  5. flask-admin 新增功能关联两张表,关联的表中的字段显示出来是对象
  6. Python Flask WTForms:如何在视图中动态禁用字段?
  7. FieldErro:无法将关键字'date_added'解析为字段。选项包括:data_ad
  8. 无法安装ndg-httpsclient或者我的解决方案错误
  9. Linux无法连接网络解决方案

随机推荐

  1. PHP中__unset()方法详解
  2. PHP中__autoload()魔术方法详解
  3. 分享几个有意思的数组方法
  4. TP5(layui)excel导入
  5. PHP中__clone()方法详解
  6. PHP生成折线图和饼图等
  7. PHP中__isset()方法详解
  8. PHP中 __set_state()方法详解
  9. php curl发送请求详细教程
  10. PHP中__set()方法详解