简体   繁体   中英

Enable/Disable input when checkbox is checked

So I have a checkbox and an "input number" and I want to enable/disable that input when check/uncheck the checkbox with jquery!!

Here is the code:

 $(document).ready(function() { 
      $("#criar").click(function() {
        $('input[name^="quant"]').each(function() {
          $(this).attr('disabled', 'disabled');
  });
  $('#myform').change(function(){
        var i=0;
        $('input[name^="tipo"]').each(function() {
          if($(this).prop('checked')){
            $('#textnumber').eq(i).prop('disabled', false);
          }else{                                         
            $('#textnumber').eq(i).prop('disabled',true);
          }
           i++;
      });
    });
  });
}); 

And the html code:

<input type="checkbox" name="tipo[]" id="tipo">
<input type="number" name="quant[]" min="1" max="50" id="textnumber[]">

This is working but only with the first checkbox and input as you can see here

Use ".is(':checked')" to check if select box is selected .Hope this helps,I think this is what you were looking for

 $(document).ready(function() { $('.number').prop('disabled', true); $('#tipo').click(function(){ if($(this).is(':checked')) { $('.number').prop('disabled', false); } else $('.number').prop('disabled', true); }) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="tipo[]" id="tipo"> <input type="number" name="quant[]" min="1" max="50" id="textnumber[]" class="number"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM