Possible defect with jQuery ':checked' selector (finding checked checkbox when there are none)
Found a strange thing. The following code is supposed to set a default value for a UI element based on whether one of a group of checkboxes (all having attribute name="privacy") is checked (in say, a form postback). If there is no value in the HTML sent by the server (the form is not a postback), the default value should be firstMenuItemKey. Otherwise, it should be the one checked in the underlying HTML.
However, while this works on the first call to the URL that includes the form, a refresh of the page will set the value of defaultPrivacyValue to one of the privacy checkboxes'. I've checked that the server isn't checking the checkbox before the script runs (it's not). I've also checked whether somewhere else in the script is checking the checkbox (it's not).
var defaultPrivacyValue = firstMenuItemKey;
$('input[name=privacy]:checked').each(function() {
defaultPrivacyValue = $(this).next().find('.name').text();
});
selectMenuItem(defaultPrivacyValue);
When I chaged the selector to 'input[name=privacy][checked=checked]' it worked as expected. Is this a defect in the jQuery implementation of 开发者_如何学GoDOM retrieval based on the ':checked' selector???? I'm using jQuery 1.4.2.
Thanks for any help you can give.
On refreshing a page, Firefox will keep the value of form fields, including the checked state of checkboxes. This could be the cause of your issue: Firefox is carrying over the checked status of your checkboxes, rather than a separate script.
I think Webkit doesn't have this behaviour, and I'm not sure about IE. Therefore a way to test this hypothesis is to check your page in Safari or Chrome.
I'm not sure why the [checked=checked] selector works differently, however.
精彩评论