简体   繁体   English

javascript focus() 不起作用,也没有设置字段值

[英]javascript focus() not working, nor setting field value

Notes Domino web form, validating onblur what was entered in a field.注释 Domino web 表单,验证在字段中输入的内容是否模糊。 Field is set as a number but I want to catch what was entered immediately if it is not a number.字段设置为数字,但如果不是数字,我想立即捕获输入的内容。 Then I want to clear what was entered and put the focus right back in the field.然后我想清除输入的内容并将焦点重新放在该字段中。 I get the code to run, and the alert comes up correctly but the focus does not happen, nor does the value get removed.我让代码运行,警报正确出现,但焦点没有发生,值也没有被删除。

function checkNumeric(fld, nm) {
    debugger;
      var x;
      x = document.getElementById(fld).value;
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {       
        document.getElementById(fld).value = '';
        alert("Non-numeric entry of '" + x + "' in : " + nm +", please try again.");
        document.getElementById(fld).focus();
      }       
    }

Be also sure that the event handler which calls this is set to prevent default.还要确保调用它的事件处理程序设置为防止默认值。 Otherwise it might be the element get the focus but is removed afterwards by the event handler emediatly.否则,它可能是元素获得焦点,但随后被事件处理程序立即删除。

            function checkNumeric(fld, nm) {
            //debugger;
            var x;
            if (typeof fld !== "string") {
                alert("fld is not a string");
            }
            if (typeof nm !== "string") {
                alert("nm is not a string");
            }

            var elm = document.getElementById(fld);
            if (elm) {
                x = elm.value;
                if (isNaN(x)) {
                    elm.value = '';
                    alert("Non-numeric entry of '" + x + "' in : " + nm + ", please try again.");
                    elm.focus();
                }
            }
        }

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

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