下面html代码中的简化表单包含许多重复的表单字段,这些字段必须通过一些jquery验证代码进行验证,也必须通过一些php代码进行处理。。。我已经在下面设置了表单字段,以便于php处理,但由于"name"标记应该是唯一的,我再次陷入了编写正确的jquery验证代码的困境。如果我想使名称标签唯一,我认为php编码会变得更加复杂。
只是想知道重复表单字段的适当结构是什么,可以方便jquery验证编码和php编码?
有什么建议吗?
提前谢谢。
html代码:
<input type="checkbox" id="check0" name="productselection[]" value="productselected0">
<input id="nrofparts0" type="text" name="nrofparts[]">
<input type="checkbox" id="check1" name="productselection[]" value="productselected1">
<input id="nrofparts1" type="text" name="nrofparts[]">
<input type="checkbox" id="check2" name="productselection[]" value="productselected2">
<input id="nrofparts2" type="text" name="nrofparts[]">
有一种方法可以做到:
HTML
<form id="theForm" method="post">
<input type="checkbox" name="checky[0]" />
<input type="checkbox" name="checky[1]" />
<input type="checkbox" name="checky[2]" />
<input type="submit" name="submit" value="submit"/>
</form>
我在数组中添加了一个键,因为这样可以更容易地确定单击了哪些复选框。你不会得到任何东西为一个未舔。这会让感到困惑
jquery:
$('#theForm input[type="checkbox"]').each(function(){
// if($(this))... some validation
});
php:
if(isset($_POST['checky'])){
foreach($_POST['checky'] as $key => $val){
echo $key.' '.$val; // this will give you the key of the checkbox, and the value
}
}
您还可以有一个对复选框进行计数的隐藏输入。这样,当您循环时,您可以使用它来设置php代码中的空复选框。
希望这能有所帮助!