jquery中怎样获得多选框中,被选择的个数

jquery中怎样获得多选框中,被选择的个数,第1张

获取 checkbox的选中个数可以直接使用如下jquery语法

$("input[type='checkbox']:checked")length;

实例演示如下

创建Html元素

<div class="box">

<span>点击按钮获取选中个数:</span><br>

<div class="content">

<input type="checkbox" name="test" >萝卜

<input type="checkbox" name="test" >青菜

<input type="checkbox" name="test" >小葱

<input type="checkbox" name="test" >豆腐

<input type="checkbox" name="test" >土豆

</div>

<input type="button" class="btn" value="获取被选择个数">

</div>

设置css样式

divbox{width:300px;height:250px;padding:10px 20px;margin:20px;border:4px dashed #ccc;}

divbox>span{color:#999;font-style:italic;}

divcontent{width:250px;height:100px;margin:10px 0;padding:5px 20px;border:2px solid #ff6666;}

input[type='checkbox']{margin:5px 10px;}

input[type='button']{width:120px;height:30px;margin:10px;border:2px solid #ebbcbe;}

编写jquery代码

$(function(){

$("input:button")click(function() {

alert($("input[type='checkbox']:checked")length);

});

})

观察效果

我建议你在每行的第一个checkbox上加一个class。这样做比较方便:

<td><input class="first" type="checkbox" /></td>

然后这样判断所有checkbox是否选中:

var checked_all = true;

for (item in $('first')) {

    if (!itemchecked) {

        checked_all = false;

    }

}

望采纳,谢谢支持!

//遍历checkbox

$( "input[type=checkbox]" )each( function() {

    if( thischecked ) {

        consolelog( "我被选了" );

    } else {

        consolelog( "我落选了" );

    }

} );

以上就是关于jquery中怎样获得多选框中,被选择的个数全部的内容,包括:jquery中怎样获得多选框中,被选择的个数、jquery 在table中有多个checkbox我要怎么才能取到第每行的第一个checkbox是否选中、如何用Jquery获取checkbox多个选项等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9777943.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存