2010-09-27 117 views
0

使用.find()如何找到一个div是否存在单选按钮或者发出警报的jQuery find命令

  <div id="emp_form"> 

      </div> 

回答

2

可以使用.find()(或只是一个descendant selector),并检查.length是这样的:

if($("#emp_form :radio").length == 0) { 
    alert("No radio buttons found!, Crap!"); 
} 

,如果你想要做的情况下,东西有单选按钮:

if($("#emp_form :radio").length > 0) { 
    //do something 
} else { 
    alert("No radio buttons found!, Crap!"); 
} 

.find()替代方案是$("#emp_form").find(":radio").length

0
if ($("#emp_form").find("input[type='radio']").length >0) { 

} else { 
    alert("There is nothing"); 
} 

固定。

+0

你的属性选择必须紧挨着'input',空格表示后裔,所以这是目前正在寻找一个'类型=“无线电”'元素* *内一个''这将不存在:) – 2010-09-27 11:23:43

1

试试这个 -

if ($('#emp_form :radio').length != 0) { 
    alert('exists'); 
    } else { 
    alert('does not exist'); 
    } 
+0

这*确实需要选择器之间的空间,无线电单元不具有*那个ID,它们*在具有该ID的单元内。 – 2010-09-27 11:24:38

+0

感谢尼克(单选按钮是div#emo_form的孩子),我忘了添加一个空格。 – Alpesh 2010-09-27 11:46:00