我想知道在下面的代码中,当我通过(document.getElementById)引用一个元素 ,并使其与验证目的的变量相等时,为什么我不能使用名称而不是name1在JavaScript中。需要帮助javascript
function validation() {
name1 = document.getElementById('name');
if (name1.value == "") {
alert('this field cannot left empty');
return false;
}
}
<form id="form" action="message.html" method="post" onSubmit='return validation()' />
<input type="text" placeholder="enter your name" id='name' />
<input type="text" placeholder="address" id='address' />
<input type="tel" placeholder="telephone" id="telephone" />
<input type="submit" value="submit">
尝试使用控制台... F12 – Akxe
你说你不能用'名=的document.getElementById( '名');'? – lU5er
是的,在这种情况下,我不能使用与元素ID相同的变量名称。 –