我想知道如何动态传递表单名称并使用它来检索表单值。我使用了下面的代码。
function validatecbr(form,outagetkt)
{
var formName = form.name;
alert(formName);
var cbr1 = formName.cbr11.value;
var cbr2 = formName.cbr22.value;
var cbr3 = formName.cbr33.value;
var cbrnum = cbr1+cbr2+cbr3;
if(cbr1.length !=3){
alert("Please Enter 10 digit Account telephone number");
formName.cbr11.focus();
return false;
}
else if(cbr2.length !=3){
alert("Please Enter 10 digit Account telephone number");
formName.cbr22.focus();
return false;
}
else if(cbr3.length !=4){
alert("Please Enter 10 digit Account telephone number");
formName.cbr33.focus();
return false;
}
else
{
alert("cbrnum "+cbrnum);
formName.method="POST";
formName.action="/support/residential/outage/serviceOutage?cbrnum="+cbrnum+"&btn="+btn+"&outagetkt="+outagetkt;
formName.submit();
}
}
以下是正文内容
<body>
<%int nc=0; %>
<div id="KMU_alerts">
Contact me with alternative notification phone number1.
<%nc++; %>
<form name="kmuform<%=nc%>" id="kmuform<%=nc%>" action="javascript:validatecbr(document.kmuform<%=nc%>,'outage');">
<input type="text" name="cbr1" id="cbr11"
onpaste="return false;" style="width: 35px;" maxlength="3"
size="2" />
<input type="text" name="cbr2" id="cbr22"
onpaste="return false;" style="width: 35px;" maxlength="3"
size="2" />
<input type="text" name="cbr3" id="cbr33"
onpaste="return false;" style="width: 35px;" maxlength="4"
size="2" />
<input id="clickMe" type="button" value="Submit" onclick="javascript:validatecbr(document.kmuform<%=nc%>,'outage');"/>
</form>
</div>
<div id="KMU_alerts">
Contact me with alternative notification phone number2.
<%nc++; %>
<form name="kmuform<%=nc%>">
<input type="text" name="cbr1" id="cbr11"
onpaste="return false;" style="width: 35px;" maxlength="3"
size="2" />
<input type="text" name="cbr2" id="cbr22"
onpaste="return false;" style="width: 35px;" maxlength="3"
size="2" />
<input type="text" name="cbr3" id="cbr33"
onpaste="return false;" style="width: 35px;" maxlength="4"
size="2" />
<input id="clickMe" type="button" value="Submit" onclick="javascript:validatecbr(this.form,'outage');"/>
</form>
</div>
</body>
但是代码在“formName.cbr11.value;”处显示“'cbr11.value' is null or not an object” 这条线。。
有人可以告诉我如何将表单名称传递给 js 函数并使用它来读取表单值。