我有以下html代码:
<form name="Register" action="Register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile(FormName='Register');" >
<p>
Name* : <input id="FirstName" type="text" name="FirstName"/> </p>
<input type="submit">
</form>
而这个JS代码:
function isEmpty(field) {
return (field == "" || field == null)
}
function validateProfile(FormName) {
var Fname = document.forms[FormName]["FirstName"].value; return false;
var g = (isEmpty(field));
alert(g);
}
问题出在这一行:
var Fname = document.forms[FormName]["FirstName"].value; 返回假;
我收到此错误消息:
未捕获的类型错误无法读取未定义的属性“名字”
如果我写而不是这一行上方的行
document.getElementById("FirstName").value
它工作得很好,所以我的问题是为什么document.forms[FormName]["FirstName"].value
不工作?