jquery中怎么选中所有的checkbox的值

jquery中怎么选中所有的checkbox的值,第1张

1、checkbox日常jquery *** 作。

现在我们以下面的html为例进行checkbox的 *** 作。

<input id="checkAll" type="checkbox" />全选

<input name="subBox" type="checkbox" />项1

<input name="subBox" type="checkbox" />项2

<input name="subBox" type="checkbox" />项3

<input name="subBox" type="checkbox" />项4

全选和全部选代码:

<script type="text/javascript">

$(function() {

$("#checkAll")click(function() {

$('input[name="subBox"]')attr("checked",thischecked);

});

var $subBox = $("input[name='subBox']");

$subBoxclick(function(){

$("#checkAll")attr("checked",$subBoxlength == $("input[name='subBox']:checked")length true : false);

});

});

</script>

checkbox属性

var val = $("#checkAll")val();// 获取指定id的复选框的值

var isSelected = $("#checkAll")attr("checked"); // 判断id=checkAll的那个复选框是否处于选中状态,选中则isSelected=true;否则isSelected=false;

$("#checkAll")attr("checked", true);// or

$("#checkAll")attr("checked", 'checked');// 将id=checkbox_id3的那个复选框选中,即打勾

$("#checkAll")attr("checked", false);// or

$("#checkAll")attr("checked", '');// 将id=checkbox_id3的那个复选框不选中,即不打勾

$("input[name=subBox][value=3]")attr("checked", 'checked');// 将name=subBox, value=3 的那个复选框选中,即打勾

$("input[name=subBox][value=3]")attr("checked", '');// 将name=subBox, value=3 的那个复选框不选中,即不打勾

$("input[type=checkbox][name=subBox]")get(2)checked = true;// 设置index = 2,即第三项为选中状态

$("input[type=checkbox]:checked")each(function(){ //由于复选框一般选中的是多个,所以可以循环输出选中的值

alert($(this)val());

});

2、radio的jquery日常 *** 作及属性

我们仍然以下面的html为例:

<input type="radio" name="radio" id="radio1" value="1" />1

<input type="radio" name="radio" id="radio2" value="2" />2

<input type="radio" name="radio" id="radio3" value="3" />3

<input type="radio" name="radio" id="radio4" value="4" />4

radio *** 作如下:

$("input[name=radio]:eq(0)")attr("checked",'checked'); //这样就是第一个选中咯。

//jquery中,radio的选中与否和checkbox是一样的。

$("#radio1")attr("checked","checked");

$("#radio1")removeAttr("checked");

$("input[type='radio'][name='radio']:checked")length == 0 "没有任何单选框被选中" : "已经有选中";

$('input[type="radio"][name="radio"]:checked')val(); // 获取一组radio被选中项的值

$("input[type='radio'][name='radio'][value='2']")attr("checked", "checked");// 设置value = 2的一项为选中

$("#radio2")attr("checked", "checked"); // 设置id=radio2的一项为选中

$("input[type='radio'][name='radio']")get(1)checked = true; // 设置index = 1,即第二项为当前选中

var isChecked = $("#radio2")attr("checked");// id=radio2的一项处于选中状态则isChecked = true, 否则isChecked = false;

var isChecked = $("input[type='radio'][name='radio'][value='2']")attr("checked");// value=2的一项处于选中状态则isChecked = true, 否则isChecked = false;

3、select下拉框的日常jquery *** 作

select *** 作相比checkbox和radio要相对麻烦一些,我们仍然以下面的html为例来说明:

<select name="select" id="select_id" style="width: 100px;">

<option value="1">11</option>

<option value="2">22</option>

<option value="3">33</option>

<option value="4">44</option>

<option value="5">55</option>

<option value="6">66</option>

</select>

看select的如下属性:

$("#select_id")change(function(){ // 1为Select添加事件,当选择其中一项时触发

//code

});

var checkValue = $("#select_id")val(); // 2获取Select选中项的Value

var checkText = $("#select_id :selected")text(); // 3获取Select选中项的Text

var checkIndex = $("#select_id")attr("selectedIndex"); // 4获取Select选中项的索引值,或者:$("#select_id")get(0)selectedIndex;

var maxIndex =$("#select_id :last")get(0)index; // 5获取Select最大的索引值

/

jQuery设置Select的选中项

/

$("#select_id")get(0)selectedIndex = 1; // 1设置Select索引值为1的项选中

$("#select_id")val(4); // 2设置Select的Value值为4的项选中

/

jQuery添加/删除Select的Option项

/

$("#select_id")append("<option value='新增'>新增option</option>"); // 1为Select追加一个Option(下拉项)

$("#select_id")prepend("<option value='请选择'>请选择</option>"); // 2为Select插入一个Option(第一个位置)

$("#select_id")get(0)remove(1); // 3删除Select中索引值为1的Option(第二个)

$("#select_id :last")remove(); // 4删除Select中索引值最大Option(最后一个)

$("#select_id [value='3']")remove(); // 5删除Select中Value='3'的Option

$("#select_id")empty();

$("#select_id")find("option:selected")text(); // 获取select 选中的 text :

$("#select_id")val(); // 获取select选中的 value:

$("#select_id")get(0)selectedIndex; // 获取select选中的索引:

//设置select 选中的value:

$("#select_id")attr("value","Normal");

$("#select_id")val("Normal");

$("#select_id")get(0)value = value;

//设置select 选中的text,通常可以在select回填中使用

var numId=33 //设置text==33的选中!

var count=$("#select_id option")length;

for(var i=0;i<count;i++)

{ if($("#select_id")get(0)options[i]text == numId)

{

$("#select_id")get(0)options[i]selected = true;

break;

}

}

通过上面的总结,应该对jquery的checkbox,radio和select有了一定的了解了吧,温故而知新,用多了就会变的熟练起来,即使有时候忘记了,也可以来翻一翻!

(1)input的checked是一个html属性,checked的值没有意义,只不过各个版本对HTML的属性值写法规定不同才有了checked="value"这种写法,只要有checked就表示页面在加载的时候checkbox被选中,没有写就页面加载的时候checkbox就不被选中。

(2)同一个页面中用js获取checkbox是否选中:documentgetElementById("checkboxId")checked

(3)jsp中在提交时,浏览器会把选中的CheckBox的Value值,添加到一个String数组当中。在Servlet(jsp)中用 String[] chk = requestgetParameterValues("CheckBox的名字");就能可到所有被选择的CheckBox值,如果没有选择则数组:chk 为null。

自己测试下就知道了

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8" />

    <title>HTML5</title>

</head>

<body>

    <form action="">

        <input class="h5course-com" id="h5courseCom1" type="checkbox" /> 

        <label for="h5courseCom1">HTML5学堂,一个神奇的网站</label>

 

        <input class="h5course-com" id="h5courseCom2" type="checkbox" checked/> 

        <label for="h5courseCom2">h5course-com</label>

 

        <input class="h5course-com" id="h5courseCom3" type="checkbox" /> 

        <label for="h5courseCom3">h5course-cn</label>

 

        <input class="h5course-com" id="h5courseCom4" type="checkbox" /> 

        <label for="h5courseCom4">h5course-cn</label>

 

        <input class="h5course-com" id="h5courseCom5" type="checkbox" checked/> 

        <label for="h5courseCom5">h5course-cn</label>

    </form>

    <script src="jquery-1113minjs" type="text/javascript"></script>

    <script type="text/javascript">

        alert('选中了这么多个:' + $('input[type=checkbox]:checked')length);

    </script>

</body>

</html>

看看上面的案例,引入jQuery,可以直接运行看效果。

思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中)。下面进行实例演示:

1、HTML结构

<input type="checkbox" name="test" value="1"/><span>1</span>

<input type="checkbox" name="test" value="2"/><span>2</span>

<input type="checkbox" name="test" value="3"/><span>3</span>

<input type="checkbox" name="test" value="4"/><span>4</span>

<input type="checkbox" name="test" value="5"/><span>5</span>

<input type='button' value='提交' onclick="fun()"/>

2、javascript代码

function fun(){

obj = documentgetElementsByName("test");

check_val = [];

for(k in obj){

if(obj[k]checked)

check_valpush(obj[k]value);

}

alert(check_val);

}

<form id="abc">

  <input type="checkbox" name="ck" />

  <input type="checkbox" name="ck" />

  <input type="checkbox" name="ck" />

  <input type="checkbox" name="ck" />

</form>var form = documentgetElementById('abc');

var input = formck; // 如果有多个相同的名字会返回dom collection(类似数组)

if(input && inputlength) {

    // dom collection

} else if (input) {

    // dom

} else {

    // 没有

}

代码如下:

var id_array=new Array();

$('input[name="id"]:checked')each(function(){

  id_arraypush($(this)val());//向数组中添加元素

});

var idstr=id_arrayjoin(',');//将数组元素连接起来以构建一个字符串

alert(idstr);

扩展资料

jQuery对checkbox的各种 *** 作

1、根据id获取checkbox

$("#cbCheckbox1");

2、获取所有的checkbox

$("input[type='checkbox']");//or

$("input[name='cb']");

3、获取所有选中的checkbox

$("input:checkbox:checked");//or

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

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

$("input:[name='ck']:checked");

以上就是关于jquery中怎么选中所有的checkbox的值全部的内容,包括:jquery中怎么选中所有的checkbox的值、JSP中如何获取checkbox的状态(选中或非选中)、jquery怎么获取多个被选中的checkbox等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存