简体   繁体   中英

Change enabled to disabled and disabled to enable the bootstrap with jquery

How do I change the input from disabled to enabled when clicks and returns from disabled to enabled when clicked

HTML

<div class="col-md-2">
    <input type="text" class="form-control" id="tes" name="tes" />
</div>
<br>
<div class="col-md-3">
    <button type="submit" id="submit1" class="glyphicon glyphicon-ok success btn btn-primary btn" value=""> </button>
</div>

JQUERY

$('#submit1').click(function() {
      $('#tes').prop("disabled",true);
    $(this).toggleClass('glyphicon glyphicon-ok').toggleClass('glyphicon glyphicon-remove btn-danger');
});

Use this Generalized function in your project to make things enabled / disabled.

 (function($) { $.fn.toggleDisabled = function(){ return this.each(function(){ this.disabled = !this.disabled; }); }; })(jQuery); $('#submit1').click(function() { $('#tes').toggleDisabled(); $(this).toggleClass('glyphicon glyphicon-ok').toggleClass('glyphicon glyphicon-remove btn-danger'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-2"> <input type="text" class="form-control" id="tes" name="tes" /> </div> <br> <div class="col-md-3"> <button type="submit" id="submit1" class="glyphicon glyphicon-ok success btn btn-primary btn" >Button </button> </div> 

You can use this one.

 $('#submit1').click(function() { $('#tes').prop("disabled",!$('#tes').prop("disabled")); $(this).toggleClass('glyphicon glyphicon-ok').toggleClass('glyphicon glyphicon-remove btn-danger'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-2"> <input type="text" class="form-control" id="tes" name="tes" /> </div> <br> <div class="col-md-3"> <button type="submit" id="submit1" class="glyphicon glyphicon-ok success btn btn-primary btn" value="Submit">SUbmit </button> </div> 

.prop(property) will return whether the property is set. You can use this to check the value and toggle it based on it's current value.

if( $('#tes').prop("disabled") )
    $('#tes').prop("disabled",false);
else
    $('#tes').prop("disabled",true);

You could reduce this to:

$('#tes').prop("disabled", !$('#tes').prop("disabled") )

This fetches the current value of the property, negates it with ! , then sets that as the new value

If what you want is to toggle the disable prop:

$('#submit1').click(function() {
      $('#tes').prop("disabled", !$('#tes').prop("disabled"));
});

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