我曾经使用这样的代码验证复选框表单:
<div>
<form action="survey.php" method="post" name="survey">
<span class="form"><input type="checkbox" name="event1" onClick="return countMarketing()">Event1</span>
<span class"form"><input type="checkbox" name="event2" onClick="return countMarketing()">Event2</span>
<span class"form"><input type="checkbox" name="event3" onClick="return countMarketing()">Event2</span>
<!-- other forms -->
</form>
</div>
还有一个类似这样的 javascript 验证(以限制选中的复选框的数量):
function countMarketing() {
var NewCountMarketing = 0
if (document.survey.event1.checked)
{NewCountMarketing = NewCountMarketing + 1}
if (document.survey.event2.checked)
{NewCountMarketing = NewCountMarketing + 1}
if (document.survey.event3.checked)
{NewCountMarketing = NewCountMarketing + 1}
if (NewCountMarketing == 3)
{
alert('Please choose only two')
document.survey; return false;
}
}
像这样的验证是有效的。但是现在,说我使用 php 来提交,如果表单的名称是这样的,我如何在 JS 中检查:
<input type="checkbox" name="events[]" id="event1" onClick="return countMarketing()">Event1
我试图将 JS 更改为:
if (document.survey.events[].checked)
{code here}
if (document.survey.getElementByName('events[]').checked)
{code here}
if (document.survey.getElementById('event1').checked)
{code here}
但这不起作用..任何人都可以对此有所了解吗?非常感谢您 :)