簡體   English   中英

單擊按鈕時執行查詢(如果選中了復選框)

[英]Execute query on button click If checkbox is checked

如果我單擊提交按鈕時選中了復選框,我想執行腳本。

我的overview.php有以下代碼段:

<table style="width: 100%; font-size: 11;">
    <tr>
        <td colspan="6">
            <form method="post" action="update.php" target="_blank">
                <script language="JavaScript">
                function toggle(source) {
                    checkboxes = document.querySelectorAll("input[name^='box[']");
                    for(var i=0, n=checkboxes.length;i<n;i++) {
                        checkboxes[i].checked = source.checked;
                    }
                }
                </script>

                <input type="submit" name="update" value="Update - LoL / BoL">
                <input type="checkbox" onClick="toggle(this)" name="All" value="All">&nbsp;Select all
            </form>
        </td>
    </tr>
    <tr>
        <th>ve067</th>
        <th>ve378</th>
        <th>ve724</th>
        <th>ve725</th>
        <th>ve726</th>
        <th>ve727</th>
    </tr>
    <tr>
        <td>
            <form method="post" action="update.php">
                <input type="checkbox" name="box[1]" value="ve067_LB1">&nbsp;LeagueBot 1<br>
                <input type="checkbox" name="box[2]" value="ve067_LB2">&nbsp;LeagueBot 2<br>
                <input type="checkbox" name="box[3]" value="ve067_LB3">&nbsp;LeagueBot 3<br>
                <input type="checkbox" name="box[4]" value="ve067_LB4">&nbsp;LeagueBot 4<br>
            </form>
        </td>
    </tr>
</table>

這是我的update.php的代碼片段:

<?php
if(isset($_POST['update'] && $_POST['box[4]']) {
    // code here
}
?>

但這對我不起作用。

解決方案:考慮以下JSFiddle

說明:這將驗證是否已至少選中復選框“ box [4]”。

這里的“鍵”代碼是下一行的return函數。

<input type="submit" name="update" value="Update - LoL / BoL" onclick="return validate();">

更新:旨在容納注釋中提供的其他信息。

請記住:此處的驗證是在客戶端完成的,因此不需要先查詢服務器( PHP

您可以按照我在pastebin上的帖子中提到的那樣進行操作

希望它對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM