Jquery Alert在焦点中出现两次

时间:2017-08-14 09:01:01

标签: javascript jquery

所以我有这个jquery:

    $("#dynamic_admin_edit_event_time").on("focusout", ".jamAkhir", function() {
      var $row = $(this).closest("tr");    // Find the row
      var row_time_id_edit = $row.find(".row_time_id_edit").text();
      var jam_awal = $row.find(".jamMulai").val();
      var jam_akhir = $row.find(".jamAkhir").val();

      if(jam_awal >= jam_akhir && jam_akhir !== '') {
        $("#edit_jam_akhir_"+ row_time_id_edit).css("border-color", "red");
        alert("Jam akhir event tidak boleh lebih dahulu dari jam mulai event");
        $(this).addClass('error');
        //$(".edit_event").prop("disabled",true);
      }

      else if(jam_akhir === ''){

      }

      else if(jam_awal < jam_akhir){
        $("#edit_jam_mulai_"+ row_time_id_edit).css("border-color", "lightgrey");
        $("#edit_jam_akhir_"+ row_time_id_edit).css("border-color", "lightgrey");
        $(this).removeClass('error');
      }
   });

所以这样做的是,每当填充类.jamakhir的文本输入时,它将检查同一行中的类.jamAwal,检查工作正常并且是正确的,但问题是警报消息这显示错误(如果.jamAwal上的值大于.jamAkhir中的值)出现两次,即使我只在if语句中放置1个警报,我如何才能使它只显示一次?

编辑:此jquery的HTML代码:

       $get_date = "SELECT tgl_event, Jam_Mulai_event, jam_akhir_event, id_jadwal_acara
                 FROM jadwal_acara
                 WHERE id_event = '$id_event'
                 AND status = '$one'";

    $result_get_date = mysqli_query($db, $get_date);
    $count_date = 1;
    while($show_date = mysqli_fetch_array($result_get_date)) {

        $tgl_event = $show_date['tgl_event'];
        $jam_mulai_event = $show_date['Jam_Mulai_event'];
        $jam_akhir_event = $show_date['jam_akhir_event'];
        $id_jadwal_acara = $show_date['id_jadwal_acara'];

        if($count_date == 1) {
            $output .= '
                                                    <tr>
                                                        <td><input type="text" name="admin_edit_exist_tgl_event[]" class="form-control edittglEvent" value="' .$tgl_event. '" required></td>
                                                        <td><input type="text" name="admin_edit_exist_jam_mulai_event[]" class="form-control jamMulai" id="edit_jam_mulai_'.$count_date.'"  value="' .$jam_mulai_event. '" style="border-color: lightgrey" required></td>
                                                        <td><input type="text" name="admin_edit_exist_jam_akhir_event[]" class="form-control jamAkhir" id="edit_jam_akhir_'.$count_date.'"  value="' .$jam_akhir_event. '" style="border-color: lightgrey" required></td>
                                                        <td style="display:none;"><input type="text" name="admin_edit_exist_id_jadwal_acara[]" class="form-control" value="' .$id_jadwal_acara. '" required></td>
                                                        <td><button type="submit" name="button_edit_add_event_time" id="edit_add_event_time" class="btn btn-success">Add More Event</button></td>

                                                    ';

            $plus = "<td class='row_time_id_edit' style='visibility: hidden;'>$count_date</td></tr>";
            $output .= $plus;
            $count_date++;
        }

        else if($count_date != 1){
            $output .= '
                            <tr id="edit_row_'.$count_date.'">
                            <td><input type="text" name="admin_edit_exist_tgl_event[]" class="form-control edittglEvent" value="' .$tgl_event. '" required></td>
                            <td><input type="text" name="admin_edit_exist_jam_mulai_event[]" class="form-control jamMulai" id="edit_jam_mulai_'.$count_date.'" value="' .$jam_mulai_event. '" style="border-color: lightgrey" required></td>
                            <td><input type="text" name="admin_edit_exist_jam_akhir_event[]" class="form-control jamAkhir" id="edit_jam_akhir_'.$count_date.'" value="' .$jam_akhir_event. '" style="border-color: lightgrey" required></td>
                            <td style="display:none;"><input type="text" name="admin_edit_exist_id_jadwal_acara[]" class="form-control" value="' .$id_jadwal_acara. '" required></td>
                            <td><button type="submit" name="button_edit_remove_exist_event_time" id="' .$count_date. '" class="btn btn-danger btn_remove_exist">Remove Event</button></td>
                            <td class="row_time_id_edit" style="visibility: hidden;">'.$count_date.'</td></tr>
                            </tr>
            ';

            $count_date++;
        }

        else{
            echo"Something Went Wrong";
        }
    }

检查时间没有问题,问题是警报出现两次,但检查没有错。

1 个答案:

答案 0 :(得分:0)

可能是因为事件冒泡,请尝试模糊&#39;而不是焦点#39;如果它仍然不起作用,请分享您的HTML代码