jquery怎么设置复选框的value值

jquery怎么设置复选框的value值,第1张

1、获取单个checkbox选中项(三种写法)

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

2、 获取多个checkbox选中项

$('input:checkbox')each(function() { if($(this)attr('checked') ==true) { alert($(this)val()); } });

3、设置第一个checkbox 为选中值

$('input:checkbox:first')attr("checked",'checked'); $('input:checkbox')eq(0)attr("checked",'true');

4、设置最后一个checkbox为选中值

$('input:radio:last')attr('checked', 'checked'); $('input:radio:last')attr('checked', 'true');

5、根据索引值设置任意一个checkbox为选中值

$('input:checkbox)eq(索引值)attr('checked', 'true'); 索引值=0,1,2 或者 $('input:radio')slice(1,2)attr('checked', 'true');

6、选中多个checkbox同时选中第1个和第2个的checkbox

$('input:radio')slice(0,2)attr('checked','true');

7、根据Value值设置checkbox为选中值

$("input:checkbox[value='1']")attr('checked','true');

8、删除Value=1的checkbox

$("input:checkbox[value='1']")remove();

9、删除第几个checkbox

$("input:checkbox")eq(索引值)remove();

索引值=0,1,2如删除第3个checkbox:

$("input:checkbox")eq(2)remove();

10、遍历checkbox

$('input:checkbox')each(function(index, domEle) {//写入代码});

11、全部选中

$('input:checkbox')each(function() { $(this)attr('checked',true); });

12、全部取消选择

$('input:checkbox')each(function() { $(this)attr('checked',false); });

JQuery对CheckBox的一些相关 *** 作

一、通过选择器选取CheckBox:

1给CheckBox设置一个id属性,通过id选择器选取:

<input type="checkbox" name="myBox" id="chkOne" value="1" checked="checked" /> $("#chkOne")click(function(){});

2给CheckBox设置一个class属性,通过类选择器选取:

<input type="checkbox" name="myBox" class="chkTwo" value="1" checked="checked" /> $("chkTwo")click(function(){});

3通过标签选择器和属性选择器来选取:

<input type="checkbox" name="someBox" value="1" checked="checked" /> <input type="checkbox" name="someBox" value="2" /> $("input[name='someBox']")click(function(){});

二、对CheckBox的 *** 作:

以这段checkBox代码为例:

<inputtype="checkbox"name="box"value="0"checked="checked"/><inputtype="checkbox"name="box"value="1"/><inputtype="checkbox"name="box"value="2"/><inputtype="checkbox"name="box"value="3"/>

1遍历checkbox用each()方法:

$("input[name='box']")each(function(){});

2设置checkbox被选中用attr();方法:

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

在HTML中,如果一个复选框被选中,对应的标记为 checked="checked"。 但如果用jquery alert($("#id")attr("checked")) 则会提示您是"true"而不是"checked",所以判断 if("checked"==$("#id")attr("checked")) 是错误的,应该是 if(true == $("#id")attr("checked"))

3获取被选中的checkbox的值:

$("input[name='box'][checked]")each(function(){if(true== $(this)attr("checked")) { alert( $(this)attr('value') ); } 或者: $("input[name='box']:checked")each(function(){if(true== $(this)attr("checked")) { alert( $(this)attr('value') ); }

$("input[name='box']:checked")与 $("input[name='box']")有何区别没试过,我试了用 $("input[name='box']")能成功

4获取未选中的checkbox的值:

$("input[name='box']")each(function(){if($(this)attr('checked') ==false) { alert($(this)val()); } });

5设置checkbox的value属性的值:

$(this)attr("value",值);

6。获取checkbox是否选中

$("input[type='checkbox']")is(':checked') 返回结果:选中=true,未选中=false

三、 一般都是创建一个js数组来存储遍历checkbox得到的值,创建js数组的方法:

1 var array= new Array();

2 往数组添加数据:

arraypush($(this)val());

3数组以“,”分隔输出:

alert(arrayjoin(','));

可以参考下面的代码:

$(document)ready(function(){

varchecked=[];

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

$('input:checkbox:checked')each(function(){

checkedpush($(this)val());

});

alert(checked);

});

});

扩展资料:

jquery参考函数

$(”元素名称”)html(”<b>new stuff</b>”); 给某元素设置内容 

$(”元素名称”)removeAttr(”属性名称”) 给某元素删除指定的属性以及该属性的值 

$(”元素名称”)removeClass(”class”) 给某元素删除指定的样式 

$(”元素名称”)text(); 获得该元素的文本 

$(”元素名称”)text(value); 设置该元素的文本值为value

参考资料来源:百度百科-jQuery

思路:通过 :checked 筛选 checkbox 选中项,然后进行遍历,利用节点关系获取到input对象,最后使用val()方法获取input的内容。实例演示如下:

1、HTML结构

<table>

    <tr>

        <td><input type="checkbox" name="test"></td>

        <td><input type="text"></td><td><input type="text"></td>

    </tr>

    <tr>

        <td><input type="checkbox" name="test"></td>

        <td><input type="text"></td><td><input type="text"></td>

    </tr>

</table>

<input type="button" value="确定">

2、jquery代码

$(function(){

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

// 找到选中行的input

var ipts = $(":checkbox:checked")parents("tr")find("input:text");  

// 遍历input并使用val()方法获取值

        str = map(function() {return $(this)val();})get()join(", ");

        alert(str);

    });

});

3、效果演示

Jquery获取 check所有选中的值

var pausedCause = '';

$(":checkbox[name='pausedCause'][checked]")each(function(){

pausedCause += thisvalue + ',';

})

pausedCause = pausedCausesubstring(0,pausedCauselength-1);

var

str;

$('chkselect')each(function(){

if

($(this)attr("checked"))

{

str+=$(this)val()+",";

}

});

str=strsubstring(0,strlength-1);

alert(str);

这样你就能得到用","拼接而成的选中的值了:例如你选中了"桔子,苹果,香蕉"

我不明白你说的影响全选反选是什么意思?就是说全选反选的checkbox不包含其中是吗?

很简单,不要和你Repeater中的checkbox的class属性设置成一样就行,就是说那个checkbox的

class不要设置成"chkselect"就可以了

1、创建如下结构的测试文件-- Content,-- jquery-1113minjs,-- JquerySelecthtml。

2、获取下拉框选中值:使用val()。

3、获取下拉框选中文本:使用find("option:selected")text()。

4、获取下拉框选中项的索引:使用get(0)selectedIndex。

5、通过value值设置下拉框的选中项:使用val("value值")。

6、通过text值设置下拉框的选中项。

7、通过value值删除下拉框的一个选项。

8、通过text值删除下拉框的一个选项。

复制的:

HTML 代码:

<form>

<input type="checkbox" name="newsletter" checked="checked" value="Daily" />

<input type="checkbox" name="newsletter" value="Weekly" />

<input type="checkbox" name="newsletter" checked="checked" value="Monthly" />

</form>

jQuery 代码:

$("input:checked")

结果:

[ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ]

var check = $("input:checked"); //得到所有被选中的checkbox

var actor_config; //定义变量

checkeach(function(i){ //循环拼装被选中项的值

actor_config = actor+','+$(this)val();

});

alert(actor_configsubstr(9)+',');

通过以下js代码去获取选中项的值,在IE7中可以正确取得选中项的值,但在IE8中却得不到选中项的值,同样在Firefox 353下也得不到值,但公司同事在Firefox其他较低版本下能正确得到值,IE6下也没有问题,⊙﹏⊙b汗

$('#permissionList-body input[name="checkboxes"][checked]')each(function(i){

alert($(this)val());

});

但是把技术代码修改为:

$('#permissionList-body input[name="checkboxes"]')each(function(i){

if(thischecked)alert($(this)val());

});===========================================================================$('#permissionList-body input[name="checkboxes"][checked]')$('#permissionList-body input[name="checkboxes"][checked='checked']')这两种写法支持IE$('#permissionList-body input[name="checkboxes"][checked='true']')

这种支持firefox

没有找到好的解决方法,只有加if(thischecked)判断了 jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 获取一组radio被选中项的值var item = $('input[@name=items][@checked]')val();获取select被选中项的文本var item = $("select[@name=items] option[@selected]")text();select下拉框的第二个元素为当前选中值$('#select_id')[0]selectedIndex = 1;radio单选组的第二个元素为当前选中值$('input[@name=items]')get(1)checked = true;获取值:文本框,文本区域:$("#txt")attr("value");多选框checkbox:$("#checkbox_id")attr("value");单选组radio: $("input[@type=radio][@checked]")val();下拉框select: $('#sel')val();控制表单元素:文本框,文本区域:$("#txt")attr("value",'');//清空内容 $("#txt")attr("value",'11');//填充内容多选框checkbox: $("#chk1")attr("checked",'');//不打勾 $("#chk2")attr("checked",true);//打勾 if($("#chk1")attr('checked')==undefined) //判断是否已经打勾单选组radio: $("input[@type=radio]")attr("checked",'2');//设置value=2的项目为当前选中项下拉框select: $("#sel")attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 $("<option value='1'>1111</option><option value='2'>2222</option>")appendTo("#sel")//添加下拉框的option $("#sel")empty();//清空下拉框// 清空所有复选框选项 $(":checkbox")attr("checked","");

以上就是关于jquery怎么设置复选框的value值全部的内容,包括:jquery怎么设置复选框的value值、jquery怎样获取多个复选框的值、如何用JQuery实现获取checkbox选中的那一行的input中的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存