admin管理员组文章数量:1335586
I want to check if a div has radio, file, checkbox. I just plete my markup in Html. I haven't any idea about this.
<div id="div1">
<input type="radio" name="rd1"> Male
<input type="radio" name="rd1"> Female
</div>
<br/>
<div id="div2">
<input type="file" name="file1">
<input type="file" name="file2">
</div>
I want to check if a div has radio, file, checkbox. I just plete my markup in Html. I haven't any idea about this.
<div id="div1">
<input type="radio" name="rd1"> Male
<input type="radio" name="rd1"> Female
</div>
<br/>
<div id="div2">
<input type="file" name="file1">
<input type="file" name="file2">
</div>
Share
Improve this question
edited Nov 10, 2017 at 10:04
Mayank Singh Fartiyal
9571 gold badge11 silver badges26 bronze badges
asked Nov 10, 2017 at 9:57
acoderacoder
7597 silver badges19 bronze badges
0
6 Answers
Reset to default 2You can use :has()
selector
if ($('div:has(:radio)').length > 0) {
//code for radio button
}
if ($('.div:has(:file)').length > 0) {
//code for file
}
if ($('.div:has(:checkbox)').length > 0) {
//code for checkbox
}
if ($('div').find('input[type="radio"], input[type="checkbox"], input[type="file"]').length > 0) {...}
Use has()
method and :radio
:checkbox
selectors, you dont need any if()
statement, check this:
$('div:has(:radio,[type=file],:checkbox)').html("This div has these inputs")
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
<input type="radio" name="rd1"> Male
<input type="radio" name="rd1"> Female
</div>
<br/>
<div id="div2">
<input type="file" name="file1">
<input type="file" name="file2">
</div>
<br>
<div id="div3">
Other inputs div
<input type="text" name="i1" value="test"><br>
<input type="number" name="i2" value="12">
</div>
Simply check if the div has any input descendents
$("div").each( function() {
var divWithInputExists = $(this).find("input").length > 0;
console.log("Does this div has inputs -> " , divWithInputExists)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
<input type="radio" name="rd1"> Male
<input type="radio" name="rd1"> Femal
</div>
<br/>
<div id="div2">
<input type="file" name="file1">
<input type="file" name="file2">
</div>
You can do it one of the following ways
Using id selector
if($('#divID :radio').length)
{
//
}
Using class selector
if($('.div-class :radio').length)
{
//
}
Using tag name selector
if($('div :radio').length)
{
//
}*emphasized text*
Radio check
if($('#div1').find(':radio').length > 0){...
File check
if($('#div2').find(':file').length > 0){...
if($('#div1').find(':radio').length > 0){
console.log('div1 has radio');
}
if($('#div2').find(':file').length > 0){
console.log('div2 has file');
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="div1">
<input type="radio" name="rd1"> Male
<input type="radio" name="rd1"> Femal
</div>
<br/>
<div id="div2">
<input type="file" name="file1">
<input type="file" name="file2">
</div>
本文标签: javascriptjQuerycheck if has radio button in the divStack Overflow
版权声明:本文标题:javascript - jQuery - check if has radio button in the div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742384265a2464709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论