JS - 检查长度函数


JS - check length function

我有代码:

function check($ile,$co,$gdzie)
{
  if (document.forms.form.$co.value.length >= $ile){
    document.forms.form.$gdzie.focus();
  }
}

而且它不起作用。错误(在谷歌浏览器中选中):

Uncaught TypeError: Cannot read property 'value' of undefined script.js:39
check script.js:39
onkeyup

这是我的 html 代码:

<p><label><?php echo TEL; ?>: <input type='text' name='tel' size='9' maxlength='9' onkeypress="keyPressNumb(event);" onKeyUp="check(9,'tel','pesel');"></label></p>
<label><?php echo PESEL; ?>:<span class='red'>*</span> <input type='text' name='pesel' size='11' maxlength="11" onKeyUp="check11();" onkeypress="keyPressNumb(event);"></label></p>

尝试document.forms.form[$co].value.length,因为在通过点表示法访问对象的值时不能使用变量。

在您的代码中,js 期望在对象document.forms.form$co属性。当您想通过变量访问属性时,必须使用括号来访问该属性。

document.bar = "hello world";
var foo = "bar";
document.foo // undefined, because document has no property "foo"
document[foo] // "hello world"