簡體   English   中英

禁用選擇是否選中復選框

[英]disable select if checkbox is checked

我有一個html / php腳本,如果未選中該復選框,則存在一個“我想被“禁用”的選擇。

這是腳本:

<input type="checkbox" name="stOne" id="stOne" value="1"/>

<select class="input" name="selectOne" id="selectOne">
    <?php
        $check_sql = mysql_query("SELECT * FROM DBtable");
        while ($check_row = mysql_fetch_assoc($check_sql))
        {
            $id = $check_row['id'];
            $st = $check_row['style'];

            echo "<option value='" . $st . "'>" . $st . "</option>";
        }
    ?>
</select>

我在堆棧上發現了許多腳本,這些腳本的解決方案在我的代碼中不起作用。

有人可以幫我弄這個嗎?

是否可以用jQuery做到這一點?

這是您在本機JS中需要的:

 window.onload = toggleSelect(); // to disable select on load if needed function toggleSelect() { var isChecked = document.getElementById("stOne").checked; document.getElementById("selectOne").disabled = !isChecked; } 
 <input type="checkbox" name="stOne" id="stOne" value="1" onClick="toggleSelect()"/> <select class="input" name="selectOne" id="selectOne"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> 

您可以使用jquery如下所述-

<input type="checkbox" name="stOne" id="stOne" value="1"/>
<select class="input" name="selectOne" id="selectOne">
    <?php
        $check_sql = mysql_query("SELECT * FROM DBtable");
                while ($check_row = mysql_fetch_assoc($check_sql))
                {
                $id = $check_row['id'];
                $st = $check_row['style'];

                echo "<option value='" . $st . "'>" . $st . "</option>";
                }
    ?>
</select>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 <script>
$( "#stOne" ).change(function() {
if ($('#stOne').prop('checked')) {
     $(".input").prop("disabled", true);
}
else
{
     $(".input").prop("disabled", false);
}

});

</script>

嘗試這個

      <input type="checkbox" name="stOne" id="stOne" value="1" onClick="disableChecked()"/>
  <select class="input" name="selectOne" id="selectOne">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
  </select>

  <script>
   window.onload = disableChecked(); 

   function disableChecked()
   {
     if($("#stOne").is(':checked')){
       $("#stOne").attr("disabled", true);
     }
    }

   </script>

暫無
暫無

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

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