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

时间:2012-07-23 14:31:08

标签: javascript forms validation

我正在进行简单的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>

1 个答案:

答案 0 :(得分:-1)

试试这个,

var user = document.myForm.un.value;

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