js表单验证中的docment.forms[“myForm”][“Element”].value命令

docment.forms["myForm"]["Element"].value command in js form validation

本文关键字:表单 Element value 命令 验证 myForm docment forms js      更新时间:2024-01-03

我正在进行一个简单的Javascript表单验证,并且在访问表单中的元素时遇到了问题

document.forms["myForm"]["un"].value;

适用于Explorer,但不适用于Firefox。我正在处理的表格是:

 <form name = "myForm" onsubmit="check()" method="POST">
            User Name:<br><input type="text" name = "un"><br/>
    Password:<br><input type="password" name = "pw"><br/>
    Confirm Password:<br><input type="password" name = "cpw"><br/>
            Email:<br><input type = "text" name = "em"><br/>
    <input type = "submit" id = "sub" value="submit">

让js验证为:

<script type = "text/javascript">
var user = document.forms["myForm"]["un"].value;
alert(user);
</script>

试试这个,

var user = document.myForm.un.value;

我不确定你是否已经听说过JQuery,但我建议你使用它http://jquery.com/.