I have many textboxes that need to be enabled only when certain action are done, such as selecting it from a drop down menu or check box. To do this, I want to disable all of the text boxes when I first open the form. And I need this to be done using one function that only disables the textboxes that I want to disable at the开发者_开发问答 beginning. How can I do this?
<input name="" type="text" class="init" disabled="disabled"/>
<input name="" type="text" class="init" disabled="disabled"/>
<input name="" type="text" class="init" disabled="disabled"/>
this will disable all text box on load
$(document).ready(function() {
$('.init').attr('disabled','disabled');
});
you can also enable text box by selecting option as below
<select name="list" id="list">
<option value="">select</option>
<option value="0">opt1(enable first text)</option>
<option value="1">opt2(enable second text)</option>
<option value="2">opt3(enable third text)</option>
</select>
script
$('#list').change(function(){
var value =$(this).val();
$('.init:eq('+value+')').removeAttr("disabled");
return false;
});
You can use this code to disable all textareas with some jQuery awesomeness.
<script type="text/javascript" src="PATH-to-JQUERY"></script>
<script>
$(document).ready(function(){
// loop through each textarea
$('textarea').each(function(){
// set it to disabled
$(this).attr('disabled', 'disabled');
});
});
</script>
To enable a textarea back, just use $('#TEXTAREA-ID-HERE').removeAttr('disabled');
The following (pure) Javascript function will disable all textboxes:
function disableTextboxes() {
var inputs=document.getElementsByTagName('input');
for(var i=0;i<inputs.length;i++) {
if(inputs[i].type=='text') {
inputs[i].disabled=1;
}
}
}
Using jQuery you could easily disable / enable form fields (or hide)
Something to think about, if you disable all fields, how will the user interact with the form to enable another form field? Not sure if that was your intention, surely not!
The code below shows how to disable all fields (you can change this to disable specific fields) and also how to show / hide based on field input
$(document).ready(function() {
//disable ALL fields
$("#theId :input").attr("disabled", true);
//specific form field value to show/hide something
$("#exampleId").change(function () {
if ($(this).val() == 'Other'){
$('#exampleIdHolder').show();
} else {
$('#exampleIdHolder').hide();
}
});
});
精彩评论