我有代码:
function check($ile,$co,$gdzie)
{
if (document.forms.form.$co.value.length >= $ile){
document.forms.form.$gdzie.focus();
}
}
它不起作用。错误(在Google Chrome中检查过):
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>
答案 0 :(得分:1)
尝试document.forms.form[$co].value.length
,因为在通过点表示法访问对象的值时无法使用变量。
在您的代码中,js期望对象document.forms.form
中的属性 $ co </ em>。如果要通过变量访问属性,则必须使用括号来访问属性。
document.bar = "hello world";
var foo = "bar";
document.foo // undefined, because document has no property "foo"
document[foo] // "hello world"