【问题标题】:Show error message when delete button is pressed without checking checkbox按下删除按钮而不选中复选框时显示错误消息
【发布时间】:2015-07-23 05:02:49
【问题描述】:

我现在已经为这个问题苦苦挣扎了一段时间。当它被选中并按下删除按钮时,它会删除该行。但是我需要在按下删除按钮时生成错误消息,而无需通过 jquery 或使用 php 函数检查复选框。

<table> 
    <tr><td><form action="" method="post" >
<table class="table table-bordered">
    <thead>
        <tr style="background-color:#404040; color: #F0F0F0 ;;">
            <td colspan=10 style="color:#fff;"><button class="btn form-control btn-info"> <a href="add.php">Add Data</a></button></td>

        </tr>
        <tr>
            <th>Select</th>
            <th>Bus Type</th>
            <th>Source</th>
            <th>Destination</th>
            <th>Departure Time</th>
            <th>Arrival Time</th>
            <th>Seats</th>
            <th>Date</th>
            <th>Price</th>  
            <th colspan="2">Edit</th>
        </tr>
    </thead>

    <?php 
        $query= "select * from bus";

        $sql= mysql_query($query);

        //echo $query;
        $count=mysql_num_rows($sql);
                /*var_dump($count)*/
        while($row=mysql_fetch_row($sql)){ 
            //var_dump($row);
            ?>
    <tbody>
        <tr>
            <td><input type="checkbox" name="checkbox[]"class= "checkbox-data "value="<?php echo $row[0];?>"></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td><?php echo $row[4]; ?></td>
            <td><?php echo $row[6]; ?></td>
            <td><?php echo $row[5]; ?></td>
            <td><?php echo $row[7]; ?></td>
            <td><?php echo $row[9];?></td>
            <td><?php echo $row[8]; ?></td>
            <td><a href="edit.php?id=<?php echo $row[0]; ?>" class="text-info">Edit</a> </td>


        </tr>
        <?php } ?>

        <tr  style="background-color:#404040; color: #F0F0F0 ";>
        <td colspan=10 style="color:#fff;"><input class="btn form-control btn-info" type="submit" name="delete" id= "delete-button" value="Delete Data"> </td>
        </tr>
    </tbody>
</table>

</form></td></tr>


    <?php 
    if(isset($_POST['delete'])){
        for($i=0; $i<$count; $i++){

            $del_id=$_POST['checkbox'][$i];
            /*var_dump($del_id);
                var_dump($_POST);
                echo $del_id."<br>";*/

            $sql="delete from bus where id=$del_id";
            echo $sql; 
            $query=mysql_query($sql);
            if($query){
                echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">";
                echo "delete success";
            }else{
                echo "delete unsuccess".mysql_error();
            }

        }

    }
?>

【问题讨论】:

  • 嘿孩子,我在这里看不到任何脚本??这真的和jQuery有关吗?
  • 这里没有发布jquery函数。我使用 jquery 尝试过,但无法解决。虽然你能给点建议吗??

标签: php jquery checkbox


【解决方案1】:

我认为这就是你想要做的。您的表单有点奇怪,因为您有一个包装链接的按钮,但无论如何,这里有一个简单的带有消息的复选框验证。为了清楚起见,我已经注释了 jQuery:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<form action="" method="post">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="1">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="2">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="3">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="4">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="5">
    <input class="btn form-control btn-info" type="submit" name="delete" id= "delete-button" value="Delete Data">
</form>
<div id="tester"></div>
<script>
    // On submission via delete button
    $("input[name='delete']").click(function() {
        // Loop through each checkbox
        $.each($("input[type='checkbox']"),function() {
            // Check if this particular one is checked
            var IsChecked   =   $(this).is(":checked");
            // If there is at least one checked, submit form
            if(IsChecked != false)
                $(this).parents("form").submit();
        });
        // Default is to write an error
        $("#tester").html("You have not checked anything to delete.");
        return false;   
    });
</script>

【讨论】:

    【解决方案2】:

    这样做

    function delete1() {
      var v = false;
      $('input[type="checkbox"]:checked').each(function() {
        v = true;
        //do some ajax work
      });
    
      if (v == false) {
        alert("please check");
      }
      $('input[type="checkbox"]:checked').closest("tr").remove();
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="t1">
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>row 1
          <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>row 2
          <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>row 3
          <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>row 4
          <td>
      </tr>
    </table>
    <input type="button" value="delete" id="d" onclick="return delete1();" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2018-06-29
      • 1970-01-01
      • 2013-01-10
      • 2017-10-22
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多