如何禁用删除所选按钮

时间:2014-01-14 10:11:21

标签: javascript button pdo

需要帮助如果复选框是emty还是提供onclick通知以至少选中复选框,如何禁用删除所选按钮?

设置所有复选框的功能............................

<script>
        function setChecked(obj) 
            {

            var check = document.getElementsByName("sel_ids[]");
            for (var i=0; i<check.length; i++) 
               {
               check[i].checked = obj.checked;
               }
        }

    </script>

我的PHP代码     

        $deleteIDs = array_map('intval', $_POST['sel_ids']) + array(0); 
        $deleteIDsSQL = implode(', ', $deleteIDs);

            $query = "DELETE FROM table_name WHERE `id` IN(".$deleteIDsSQL.")";
            $delete = $dbh->prepare($query);
            $delete->execute();
    }

    print "
    <form method=\"POST\">
    <table style=\"width:100%\" class=\"list\">
      <tr>
        <th style=\"width:5%\"><input type=\"checkbox\" name=\"set\" onclick=\"setChecked(this)\"></th>
        <th style=\"width:45%\">Email</th>
        <th style=\"width:15%\">Subject</th>
        <th style=\"width:35%\">Message</th>
        <th style=\"width:35%\">Date</th>
      </tr>";

    $query = "SELECT * FROM table_name";
    $stmt = $dbh->prepare($query);
    $stmt->execute();
    $stmt_number_of_rows = $stmt->rowCount();
        if (!$stmt_number_of_rows) {
            print "
      <tr>
        <td colspan=\"4\" style=\"text-align:center;color:#EE0000;padding:15px\">No Records</td>
      </tr>";
        } 
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
                $date = "<span style=\"color:#8B8B00\">".date($configs['DateFormat'], $row['date'])."</span>";
                print "
      <tr>
        <td style=\"text-align:center\"><input type=\"checkbox\" name=\"sel_ids[]\" value=\"{$row['id']}\"></td>
        <td>".$row['email']."</td>
        <td style=\"text-align:center\">".$row['email']."</td>
        <td style=\"text-align:center\">".$row['message']."</td>
        <td style=\"text-align:center\">".$date."</td>
      </tr>"; 
            }  
    print "
    </table>
    <div style=\"padding:3px 3px 7px\">
      <input type=\"submit\" name=\"delete\" onclick=\"return confirm('Are You Sure You Want To Delete The Selected Messages?');\" value=\"Delete Selected\" />
    </div>
    </form>"; 
    ?>

2 个答案:

答案 0 :(得分:0)

添加ID以提交按钮

<input type=\"submit\" name=\"delete\" onclick=\"return confirm('Are You Sure You Want To Delete The Selected Messages?');\" value=\"Delete Selected\" id=\"delete_button\" />

<script>
    function setChecked(obj) 
    {
        if(setChecked.totalChecked === undefined){
            setChecked.totalChecked = 0;
        }
        if(obj.checked === true){
            setChecked.totalChecked++;
        }else{
            setChecked.totalChecked--;
        }

        if(setChecked.totalChecked == 0 ){
            document.getElementById("delete_button").disabled = true;
        }else{
            document.getElementById("delete_button").disabled = false;
        }

    }
</script>

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
    function setChecked(obj) 
    {
        if(setChecked.totalChecked === undefined){
            setChecked.totalChecked = 0;
        }
        if(obj.checked === true){
            setChecked.totalChecked++;
        }else{
            setChecked.totalChecked--;
        }
        if(setChecked.totalChecked == 0 ){
            document.getElementById("delete_button").disabled = true;
        }else{
            document.getElementById("delete_button").disabled = false;
        }
    }
</script>
</head>
<body>
<input type="checkbox" onchange="setChecked(this)">1<br>
<input type="checkbox" onclick="setChecked(this)">2<br>
<input type="checkbox" onclick="setChecked(this)">3<br>
<input type="checkbox" onclick="setChecked(this)">4<br>
<input type="checkbox" onclick="setChecked(this)">5<br>
<input type="checkbox" onclick="setChecked(this)">6<br>
<input type="checkbox" onclick="setChecked(this)">7<br>
<input disabled="disabled" type="submit" name="delete" onclick="return confirm('Are You Sure You Want To Delete The Selected Messages?');" value="Delete Selected" id="delete_button" />
</body>
</html>