问题
With the below sample page I get a different output for IE than I do for other browsers.
<html>
<head>
<title>hidden fields test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form>
<input type="hidden" value="hidden1" />
<input type="hidden" value="hidden2" />
<input type="text" value="text1" />
<input type="text" value="text2" />
</form>
<script type="text/javascript">
$(function() {
var inputs = $("form input:enabled");
var concatenated = '';
inputs.each(function() {
concatenated = concatenated + $(this).val();
});
alert(concatenated);
});
</script>
</body>
</html>
With IE 8 (8.0.7600.16385), the output is "text1text2", with Chrome (10.0.648.127) and Firefox (3.6.13) it is "hidden1hidden2text1text2". This was unexpected to me. Is this a bug in IE or jQuery, or just an expected difference in browsers that jQuery is not accounting for?
Do hidden fields in IE always carry an implicit "disabled"?
回答1:
It seems that FF and Chrome assume that even if an element is hidden if it is not disabled it is considered enabled. In IE it seems that assumption is not made and the hidden input is neither considered enabled or disabled. Neither $('input:enabled') or $('input:disabled') runs with hidden elements in IE:
Simple test here: http://jsfiddle.net/KFu4t/4/
Both FF and Chrome show the input as enabled but IE doesn't show it enabled OR disabled.
It is noted on the jQuery comments for :enabled that:
It's not stated specifically in the docs, but this doesn't seem to include inputs of type "hidden".
http://api.jquery.com/enabled-selector/
回答2:
change this line:
var inputs = $("form input:enabled");
to:
var inputs = $("form input:not(:disabled)");
来源:https://stackoverflow.com/questions/5277703/hidden-fields-in-ie-are-considered-disabled