Notes Domino Web 表单,验证 onblur 字段中输入的内容。字段设置为数字,但如果它不是数字,我想立即捕获输入的内容。然后我想清除输入的内容并将焦点放回该字段。我让代码运行,警报正确出现,但焦点没有发生,值也没有被删除。
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();
}
}
最佳答案
还要确保调用此事件的事件处理程序已设置为防止默认。否则,可能是该元素获得焦点,但随后被事件处理程序立即删除。
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();
}
}
}
关于javascript focus() 不起作用,也不设置字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997137/