问题
Is it possible to do something like this to check if radio form is unchecked:
if !($(this).find("input:checked")) {}
what would be the correct syntax?
回答1:
Try this:
$(this).find("input:not(:checked)")
回答2:
Try this instead bc .find
returns an array of DOM elements:
if ($(this).find("input:checked").length == 0) {}
回答3:
You can use .is
for boolean test
if (!$(this).find('input').is(':checked')) { }
回答4:
First of all, you'll want that !
inside the conditional check:
if (!$(this).find("input:checked")) {}
Second of all, the .find method returns a jQuery object, so even if it found nothing, it will still be truthy.
What you'll probably want to check is the length of the jQuery object:
if (!$(this).find("input:checked").length) {}
If there are no elements, then etc etc etc
回答5:
I presume you're searching a parent element (e.g. a form) to see if you can find descendant checkboxes that are checked.
You want something like this:
if (!$(this).find('input:radio:checked').length) {
The condition passes if there are no descendant radio input elements that are checked.
来源:https://stackoverflow.com/questions/6048727/jquery-using-to-say-not-selected