Javascript 放置名称[]时复选框的JS验证不起作用
我有一个JS验证复选框Javascript 放置名称[]时复选框的JS验证不起作用,javascript,validation,Javascript,Validation,我有一个JS验证复选框 function ActionCcdCheck (theForm) { if (theForm.ccd_chk.checked) { theForm.ccd_pos[0].className = 'part'; theForm.ccd_pos[1].className = 'part'; theForm.ccd_pos[2].className = 'part'; theForm.ccd_p
function ActionCcdCheck (theForm)
{
if (theForm.ccd_chk.checked)
{
theForm.ccd_pos[0].className = 'part';
theForm.ccd_pos[1].className = 'part';
theForm.ccd_pos[2].className = 'part';
theForm.ccd_pos[0].disabled = false;
theForm.ccd_pos[0].checked = false;
theForm.ccd_pos[1].disabled = false;
theForm.ccd_pos[1].checked = false;
theForm.ccd_pos[2].disabled = false;
theForm.ccd_pos[2].checked = false;
}
else
{
theForm.ccd_pos[0].disabled = true;
theForm.ccd_pos[1].disabled = true;
theForm.ccd_pos[2].disabled = true;
}
}
复选框
<td colspan="1" rowspan="2" width="35">CCD</td>
<td>Check</td>
<td><input type="checkbox" name="ccd_chk" value="yes" class="part" onclick="ActionCcdCheck (this.form);" onkeypress="FocusChange (this.form, 5, 4);"/> Yes</td>
<tr>
<td>Position</td>
<td>
<input type="checkbox" name="ccd_pos[]" value="front" class="part" onkeypress="FocusChange (this.form, 6, 3);"/> Front
<input type="checkbox" name="ccd_pos[]" value="back" class="part" onkeypress="FocusChange (this.form, 7, 2);"/> Back
<input type="checkbox" name="ccd_pos[]" value="fb" class="part" onkeypress="FocusChange (this.form, 8, 1);"/> FB
</td>
<tr>
CCD
检查
对
位置
正面
返回
食品饮料
现在,当我将复选框名称设置为name=“ccd_pos[]”时,我面临着一个问题。JS验证不起作用。我之所以使用它,是因为我想提交多个值的复选框
function ActionCcdCheck (theForm)
{
if (theForm.ccd_chk.checked)
{
theForm.ccd_pos[0].className = 'part';
theForm.ccd_pos[1].className = 'part';
theForm.ccd_pos[2].className = 'part';
theForm.ccd_pos[0].disabled = false;
theForm.ccd_pos[0].checked = false;
theForm.ccd_pos[1].disabled = false;
theForm.ccd_pos[1].checked = false;
theForm.ccd_pos[2].disabled = false;
theForm.ccd_pos[2].checked = false;
}
else
{
theForm.ccd_pos[0].disabled = true;
theForm.ccd_pos[1].disabled = true;
theForm.ccd_pos[2].disabled = true;
}
}
谁能给我一个建议?谢谢。在您的代码中:
theForm.ccd_pos[0]
正在查找表单的ccd\u pos
属性,但没有。您需要的是:
theForm['ccd_pos[]'][0]
var box, boxes = theForm.['ccd_pos[]'];
for (var i=0, iLen=boxes.length; i<iLen; i++) {
box = boxes[i];
if (theForm.ccd_chk.checked) {
box.className = 'part';
box.disabled = false;
box.checked = false;
} else {
box.disabled = true;
// and probably
box.className = '';
}
}
由于具有相同名称的表单控件将作为集合返回,因此:
theForm['ccd_pos[]']
返回三个控件中的一个,其名称为ccd\u pos[]
。然后使用普通索引名访问集合的各个成员
请注意,在javascript中,只能在名称符合的规则的情况下使用。否则,必须使用方括号(正式)表示法
因此,您的代码可以是:
theForm['ccd_pos[]'][0]
var box, boxes = theForm.['ccd_pos[]'];
for (var i=0, iLen=boxes.length; i<iLen; i++) {
box = boxes[i];
if (theForm.ccd_chk.checked) {
box.className = 'part';
box.disabled = false;
box.checked = false;
} else {
box.disabled = true;
// and probably
box.className = '';
}
}
var-box,box=theForm.[ccd_pos[]];
对于(var i=0,iLen=box.length;iYou可能最好遍历表单中的checkbox元素,而不是查看第n个元素。例如jquery$(输入:checkbox)。each(函数(){//blah;});请注意@Duniyadnd的示例代码在选择器周围缺少引号;它应该是$('input:checkbox')…
非常酷。谢谢,现在我的代码运行良好。:)干杯@RobG