Enable and Disable Textboxes jquery
I have enabled mutiple textboxes with jQuery. But I am not sure how to re-enable them if it the radio button is not selected. What modifications would I have to make to my current code?
<script type="text/javascript">
$(document).ready(function() {
$(":radio[id^=check]").click(function() {
var textId = "#" + this.id.replace("check", "text");
$(textId).removeAttr("disabled", "disabled").css('background-color', '#99FF66')
});
//alert("test开发者_高级运维");
});
</script>
Replace $(textId).removeAttr("disabled", "disabled")
by $(textId).removeAttr("disabled")
It is quite obvious if you read the docs, removeAttr
doesn't take two arguments
You could use a single handler to do both -- disable when the radio button isn't checked, enable when it is. Using @AlienWebGuy's observation that you can use the checked test for the value of the property, this can be done as:
$(":checkbox[id^=check]").click(function() {
$("#" + this.id.replace("check", "text"))
.prop("disabled", $(this).filter(':checked').length == 0);
});
when coupled with this CSS
<style>
input:disabled {
background-color: #99ff66;
}
</style>
You want to manipulate the property, not the attribute:
$(textId).prop('disabled',true);
and $(textId).prop('disabled',false');
I was able to complete the task doing it this way. Baiscally, I have id on my textbox and radio buttons. That go radio button id="check1" textbox id="text1", radio button id="check2" textbox id="text2". Then in my decline id i have radio button id="checker1" and textbox id="text1", etc.
So based on which one the choose it will enable it and turn it green, or disable and turn it red.
Here is the code.
<script type="text/javascript">
$(document).ready(function() {
//Goes through each and if click adds css and removes disable
$(":radio[id^=check]").click(function() {
var textId = "#" + this.id.replace("check", "text");
$(textId).removeAttr("disabled").css('background-color', '#99FF66').css('color', '#000')
});
//Goes through each and if checked adds disabled and turns red
$(":radio[id^=checker]").click(function() {
var textId = "#" + this.id.replace("checker", "text");
$(textId).attr("disabled", "disabled").css('background-color', '#FF0033').css('color', '#FFFFFF')
});
//alert("test");
});
</script>
精彩评论