dounai6613 2017-07-15 17:45
浏览 36
已采纳

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

I am generating a dynamic form, and just want to insert JavaScript validation in it.

<?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>";
?>

This is my JavaScript validation

<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>

The problem is that the validation is working for the first entry generated but not for others. How can I fix it?

  • 写回答

1条回答 默认 最新

  • dousi6405 2017-07-15 18:23
    关注

    have you tried passing in the id of the form along with the function and then accessing the form through that Id?

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

    the above is for your forms,

    then in your js,

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

    Now that we have the ID of the form, refer to Best Practice: Access form elements by HTML id or name attribute? This explains how to access the form elements from it's ID

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题