最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - disable select if checkbox is checked - Stack Overflow

programmeradmin1浏览0评论

I have a html/php script where there is a Select I want to be "disabled" if checkbox is not checked.

This is the script:

<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>

I found many scripts here on stack overflow with a solution that didn't work in my code.

Can someone help me with this?

Is it possible to do this NOT with jQuery?

I have a html/php script where there is a Select I want to be "disabled" if checkbox is not checked.

This is the script:

<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>

I found many scripts here on stack overflow with a solution that didn't work in my code.

Can someone help me with this?

Is it possible to do this NOT with jQuery?

Share Improve this question edited Jun 6, 2016 at 9:55 ADreNaLiNe-DJ 4,9193 gold badges27 silver badges35 bronze badges asked Jun 6, 2016 at 9:52 NickiJeyNickiJey 273 silver badges7 bronze badges 5
  • without JQuery, you won't be able to enable it again if the checkbox is checked again. Is it expected behavior? – Prashant Commented Jun 6, 2016 at 9:55
  • how can you know checkbox is checked or not – D Coder Commented Jun 6, 2016 at 9:57
  • By runtime or initially ? – Rayon Commented Jun 6, 2016 at 10:01
  • @Prashant Jquery is born out of Plain Javascript, so javascript can do anything jquery is capable of. – Anupam Commented Jun 6, 2016 at 10:07
  • I know that. Thank you. I asked, cuz OP specifically refused for JQuery in his post. It was not mentioned to use JS as even he is trying to achieve it via PHP script. – Prashant Commented Jun 6, 2016 at 10:28
Add a ment  | 

3 Answers 3

Reset to default 6

Here is what you need in native 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>

You can do this using jquery as mentioned below -

<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./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>

try this

      <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>
发布评论

评论列表(0)

  1. 暂无评论