检查jQuery是否存在单选按钮? [英] Check if a radio button exists with jQuery?
问题描述
我想检查jQuery是否存在某个 radio
按钮,我有以下内容:
I want to check if a certain radio
button exists with jQuery, I have this below:
if (jQuery('input[name=list_in]', '#sell_form').length) {
return true;
}
这有效,但即使该字段不是无线电字段,它也会返回。如果字段名为 list_in
并且是收音机
,我需要它返回true字段。
This works, but it will return even if the field is not a radio field. I need it to return true ONLY if the field is named list_in
and is a radio
field.
推荐答案
你可以这样做:
if (jQuery('input[type=radio][name=list_in]', '#sell_form').length) {
return true;
}
来自:radio 选择器文档:
因为:radio 是一个jQuery扩展而不是CSS
规范的一部分,查询使用:radio无法利用本机DOM提供的
性能提升 querySelectorAll()
方法。要在现代浏览器中获得更好的性能,请使用 [type =radio]
代替。
Because :radio is a jQuery extension and not part of the CSS specification, queries using :radio cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="radio"] instead.
这篇关于检查jQuery是否存在单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!