I'm in a situation where I have to use the disabled attribute to deactivate all inputs I don't want the user to edit.
<input disabled="<%= disableInputs%>" type="text"></input>
that renders
<input disabled="False" type="text"></input>
or
<input disabled="True" type="text"></input>
This works fine on Chrome and FF, but on IE it does not.
Now I'm trying to remove those attributes (where disabled="False") with javascript, but I don't get the expected results. Againt, it the javascript works gor Chrome and FF, but not for IE (using 8).
at the end it should look like:
<input type="text"></input>
or
<input disabled="True" type="text"></input>
my javascript looks like (i've tried almost any combination):
$('document').ready(function () {
$("input[disabled='false']").each(function(){$(this).attr('disabled', false);});
$("select[disabled='false']").each(function(){$(this).attr('disabled', false);});
$("input[disabled='False']").each(function(){$(this).attr('disabled', false);});
$("select[disabled='False']").each(function(){$(this).attr('disabled', false);});
$("input[readonly='false']").each(function(){$(this).attr('readonly', false);});
$("select[readonly='false']").each(function(){$(this).attr('readonly', false);});
$("input[readonly='False']").each(function(){$(this).attr('readonly', false);});
$("select[readonly='False']").each(function(){$(this).attr('readonly', false);});
$("input[disabled='false']").each(function(){$(this).removeAttr('disabled');});
$("select[disabled='false']").each(function(){$(this).removeAttr('disabled');});
$("input[disabled='False']").each(function(){$(this).removeAttr('disabled');});
$("select[disabled='False']").each(function(){$(this).removeAttr('disabled');});
$("input[readonly='false']").each(function(){$(this).removeAttr('readonly');});
$("select[readonly='false']").each(function(){$(this).removeAttr('readonly');});
$("input[read开发者_运维百科only='False']").each(function(){$(this).removeAttr('readonly');});
$("select[readonly='False']").each(function(){$(this).removeAttr('readonly');});
});
A live example at: http://jsfiddle.net/2LNa6/ UPDATED
You should use the .prop method in jQuery as it does sanity checking for you bypassing these annoying IE errors. I can see you're using jQuery 1.6, so this should work:
$('document').ready(function () {
//get each input that is disabled
$('input').each(function(i, el){
//see if it should be disabled (true|false)
var disabled = $(el).data('disabled');
$(el).prop('disabled', disabled);
});
});
Here is the updated jsFiddle for you to see.
You have a javascript error. Add ); to the last line of your javascript and it will fix it.
$('document').ready(function () {
$("input[disabled='False']").each(function(){$(this).removeAttr('disabled');});
$("input[disabled='false']").each(function(){$(this).removeAttr('disabled');});
}**)**
You have missed out the closing brace (highlighted in bold)
The actual code should be
$('document').ready(function () {
$("input[disabled='disabled']").each(function(){$(this).removeAttr('disabled');});
})
And instead of False, pass disabled. That should work fine in all browsers
Use a attribute called key to identify whether to enable the input or not. Here is the code. It will work in all browsers.
<table class="tabla" cellpadding="0" border="0">
<tr>
<td>
<label>Names</label>
</td>
<td>
<label>DoB</label>
</td>
<td style="width:82px" >
<label>Gender</label>
</td>
</tr>
<tr id="HijoCargoRow0">
<td>
<input key="false" id="XXX" name="XXX" style="width:260px" type="text" value="" />
</td>
<td>
<input class="datepicker" key="true" id="YYY" name="YYY" style="width:70px" type="text" value="" />
</td>
<td>
<input key="true" id="M" name="ZZZ" type="radio" value="M" /><label for="M">M</label>
<input key="true" id="F" name="ZZZ" type="radio" value="F" /><label for="F">F</label>
</td>
</tr>
</table>
<script>
$(function(){
$("input[key='true']").each(function(){
$(this).attr('disabled','disabled')
});
});
</script>
You can try a combination of the filter
selector and prop
method in jQuery
to remove the disabled
attribute from all input
tags:
$("input").filter(function () {
return $(this);
}).prop("disabled'", false);
http://api.jquery.com/filter/
精彩评论