javascript focus()无效,也未设置字段值

问题描述 投票:0回答:1

注意Domino Web表单,用于验证在字段中输入的内容。字段设置为数字,但是如果不是数字,我想立即捕获输入的内容。然后,我想清除输入的内容,然后将焦点重新放在字段中。我使代码运行,并且警报正确发出,但是焦点没有发生,也没有删除值。

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();
      }       
    }
javascript lotus-domino
1个回答
1
投票

还请确保将调用此事件的事件处理程序设置为防止默认设置。否则,它可能是该元素获得了焦点,但随后被事件处理程序立即删除。

            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();
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.