使用Javascript以两种不同的方式验证动态创建的数组值

时间:2017-08-18 11:37:14

标签: javascript jquery jquery-validate

这是我的HTML代码:

<tr>
   <td colspan="3" class="table-success">
      <div class="ck">
      <?php echo $sno=$sno+1; ?><input type="hidden" id="sno" value="<?php echo $sno;?>"/>
   </td>
   <td colspan="3" class="table-success">
      <div class="ck">
      <input readonly name="emp_code[]" id="emp_code[]" value="<?php echo $row['emp_code']?>" class="form-control">
   </td>
   <td colspan="3" class="table-success">
      <div class="ck">
      <input readonly name="emp_name[]" id="emp_name_<?php echo $sno;?>" value="<?php echo $row['emp_name']?>" class="form-control">
   </td>
   <td colspan="3" class="table-info">
      <div class="ck">
      <input readonly name="emp_mailid[]" id="emp_mailid_<?php echo $sno;?>" value="<?php echo $row['emp_mailid']?>" class="form-control">
   </td>
   <td colspan="3" class="table-info">
      <div class="ck">
      <input readonly name="nomination[]" id="nomination_<?php echo $sno;?>" value="<?php echo $row['nomination']?>" class="form-control">
   </td>
   <td colspan="3" class="table-info">
      <div class="ck">
      <label><input type="radio" name="attendance[<?php echo $nextRowId ?>]" id="attendance[<?php echo $nextRowId  ?>]" value="1"/>Yes</label> <label><input type="radio" name="attendance[<?php echo $nextRowId  ?>]" id="attendance[<?php echo $nextRowId ?>]" value="0">No</label>
   </td>
</tr>

这是我的Javascript代码:

<script type="text/javascript">
//function to add new row
jQuery(function() {
  var counter = 1;
  var nextRowId=($('#tb tr').length)-1;
 //number of rows fetched from database
  jQuery('a.addrow').click(function(event) {
    event.preventDefault();

    var newRow = jQuery('<tr><td colspan="3" class="table-active">'+nextRowId+'</td><td colspan="3" class="table-active"><input type="text" name="emp_code[]" id="emp_code[]"  class="form-control"/></td><td colspan="3" class="table-success"><input type="text" name="emp_name[' + nextRowId + ']" id="emp_name[' + nextRowId + ']" class="form-control" required/></td><td colspan="3" class="table-info"><input type="text" name="emp_mailid[' + nextRowId + ']" id="emp_mailid[' + nextRowId + ']" class="form-control" required/></td><td colspan="3" class="table-info"><select required class="form-control" id="nomination[' + nextRowId + ']" name="nomination[' + nextRowId + ']"> <option value="">Select</option><option value="Nominated">Nominated</option><option value="Invited">Invited</option><option value="Both">Both</option><option value="None">Neither nominated nor invited</option></select></td><td colspan="3"><label><input type="radio" name="attendance[' + nextRowId + ']" id="attendance[' + nextRowId + ']" value="1" required>Yes</label><label><input type="radio" name="attendance[' + nextRowId + ']" id="attendance[' + nextRowId + ']" value="0">No</label></td><td><a href="javascript:void(0);" class="remove" onclick=><span class="glyphicon glyphicon-remove"></span></a></td></tr>');
    counter++;
     nextRowId++;
    jQuery('table.table').append(newRow);
    j++;
  //Init bootstrapToggle
  $('.toggleone').bootstrapToggle({
    on: 'Yes',
    off: 'No'
  });
  });
}); 

</script>

单击按钮添加新行时,将调用此函数 现在我想验证整个数组,即emp_code[0]....emp_code[n] 当我使用验证码时,它不起作用。 这是我的jQuery:

<script type="text/javascript">
$().ready(function () {
  // validate signup form on keyup and submit
  $("#form1").validate({
    rules: {
      training_date: "required",
      from_time:"required",
      to_time:"required",
      venue:"required",
     'emp_code[]': {
                required: true,
     }
    },
    messages: {
    },
    submitHandler: function (form) { // for demo
      alert('valid form');
      return false;
    }
  });

});
</script>

我需要在jQuery中使用表单验证来验证完整的数组。有许多类似的问题,但它与相同的数组名称不同,例如emp_code[]它以有序的方式存储所有值,而之前的所有问题都没有。 Ii尝试了这些答案,但没有成功,所以我提出了我的问题。

0 个答案:

没有答案