我有一个程序,它从数据库中存储的数据输出一组3组字段 我能够使用for循环打印它,但验证仅适用于第一组字段,而其他剩余2组字段未经验证。
以下是我的程序的代码和屏幕截图。
edit.php
<?php for($q=0;$q<count($arr_search['response'][0]['add_info']);$q++){?>
<div class="present_fields_1">
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Type of work:</label>
<div class="col-md-4">
<select id="type_of_work[]" name="type_of_work[]" class="form-control" style="width: 70%;">
<option value="Audit Report">Audit Report</option>
<option value="ITR filing">ITR filing</option>
<option value="VAT Filing">VAT Filing</option>
<option value="Accounting">Accounting</option>
<option value="Registration">Registration</option>
<option value="Certification">Certification</option>
<option value="Others">Others</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Status:</label>
<div class="col-md-4">
<select id="status" name="status[]" class="form-control" style="width: 70%;">
<option value="<?php echo $arr_search['response'][0]['add_info'][$q]['status']; ?>"><?php echo $arr_search['response'][0]['add_info'][$q]['status']; ?></option>
<option value="Pending">Pending</option>
<option value="Work in process">Work in process</option>
<option value="Completed">Completed</option>
</select>
</div>
</div>
<!--date-->
<div class="form-group row">
<label for="example-date-input" class="col-2 col-form-label" style="margin-left:28.5%;">DATE:</label>
<div class="col-10">
<input class="form-control datepicker p" id="date[]" name="date[]" value="<?php echo $arr_search['response'][0]['add_info'][$q]['date']; ?>" type="text" readonly style="width: 70%;">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Comment:</label>
<div class="col-md-4">
<input id="commentss" name="comment[]" value="<?php echo $arr_search['response'][0]['add_info'][$q]['comment']; ?>" type="text" placeholder="" class="form-control input-md" style="width: 70%;" >
</div>
</div>
<a href="#" class="remove_field_pre1" style="">
<img src="images/del24.png">
</a>
</div>
<?php }?>
<script type="text/javascript">
$(document).ready(function () {
$("#status").click(function () {
if ($("#status").val() == "Completed") {
$("#commentss").attr("required", "required");
}
else
$("#commentss").attr("required", false);
});
});
</script>
正如您所看到的,代码的最后一部分,即Javascript仅适用于第一组而不是代码的剩余部分。
答案 0 :(得分:0)
问题在于:
$("#status").click(function () {
这里使用的是#
即Id选择器,它们在页面中是唯一的。而是使用.
选择器并使用$.each()
迭代对象并检查每个元素是否有效。
答案 1 :(得分:0)
<select id="status<?php echo $q?>"
<input id="commentss<?php echo $q?>"
for(int i=0; i<3; i++){
$("#status"+i).click(function () {
if ($("#status"+i).val() == "Completed") {
$("#commentss"+i).attr("required", "required");
}
else
$("#commentss"+i).attr("required", false);
});
}