禁用选择是否选中复选框

时间:2016-06-06 09:52:37

标签: javascript php html

我有一个html / php脚本,如果没有选中复选框,则选择我想要“禁用”。

这是剧本:

{{1}}

我在堆栈上发现了许多脚本溢出的解决方案在我的代码中无效。

有人可以帮我这个吗?

是否可以不使用jQuery执行此操作?

3 个答案:

答案 0 :(得分:3)

以下是本机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>

答案 1 :(得分:0)

您可以使用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>

答案 2 :(得分:0)

试试这个

      <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>
相关问题