以动态生成的形式插入JavaScript验证

时间:2017-07-15 17:45:02

标签: javascript php validation

我正在生成动态表单,只想插入JavaScript 验证。

<?php
echo'<form action="" nam="airform" onsubmit="return check()">';

while ($row=$run->fetch_assoc()) {

    if (empty($array_vendor)) {
            echo "<option>NO ENTRIES FOUND!!</option></select></td>";
    }
    else {

       echo "<option value=''>Select</option>";

       for($i=0;$i<count($array_vendor);$i++){
           echo "<option> $array_vendor[$i]
           $array_phone[$i] ,VENDOR =  $array_name[$i]</option>";

       }
   }
   echo "</select></td><td><select name='carnumber' class='form-control'style='width:100px'class='nametwo'>";

   if (empty($array_car)) {
       echo "<option>NO ENTRIES FOUND!!</option></select></td>";
   }
   else {
       # code...
       echo "<option value=''>Select</option>";

       for($i=0;$i<count($array_car);$i++){

            echo "  <option> $array_car[$i],
             $array_belong[$i]</option>";

       }
   }
   echo "</select></td>";
   echo "<td><select name='status_option' class='form-control' class='namethree'>
              <option value=''>Select</option>
              <option>Completed</option>
              <option>Pending</option>
              <option>Cancelled</option>
              <option>Refunded</option>
          </select></td>
          <td><input type='submit' name='air_status' class='ui huge olive basic button' value='Update'></td>
          <td><input type='hidden' name='id_one' value='$row[id]'></td>
        </tr></form>";
?>

这是我的JavaScript验证

<script type="text/javascript">
    function check() {

        var input = document.forms['airform'].driverentry.value;
        var y = document.forms['airform'].carnumber.value;
        var z = document.forms['airform'].status_option.value;

        var fields = input.split(/=/);

        var name = fields[0];
        var street = fields[1];


        var a = y.split(/,/);

        var noneed = a[0];
        var need = a[1];


        if (input === "") {
            alert('Please select the driver detail');
            $(this).focus();
            return false;
        } else if (y === "") {
            alert('Please select the car number');
            $(".nametwo").focus();
            return false;
        } else if (z === "") {
            alert('Please select the status');
            $(".namethree").focus();
            return false;
        } else if (need != street) {
            alert('Vendor email on both the fields should match');
            $(".nametwo").focus();
            return false;
        } else {
            return true;
        }
    }
</script>

问题在于验证适用于生成的第一个条目,但不适用于其他条目。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您是否尝试过传入表单的id以及函数,然后通过该Id访问表单?

<?php $i = 0; 
//looping logic
?>
<form id="someID<?php echo $i ?>" onsubmit="check(this.id)">

以上是适用于您的表格,

然后在你的js中,

function check(id){
   var form = document.getElementById(id);
   //logic
}

现在我们有了表单的ID,请参阅 Best Practice: Access form elements by HTML id or name attribute? 这解释了如何从其ID

访问表单元素