jQuery, using ! to say “not selected”

ⅰ亾dé卋堺 提交于 2020-04-30 07:48:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!