您的位置:首页 > Web前端 > JQuery

Jquery实现点击某一checkbox时,value类似的checkbox也选中

2015-02-27 09:26 411 查看
<script
type="text/jscript">

//点击某checkbox时,把相关的上传文件及生成文件一并删除。2012.2.15 jb
$(document).ready(function(){
$(":checkbox").click(function(){
if(this.checked) {
var id=$(this).val();
id=id.substring(0,id.length-4);
if(id.indexOf("_Original") >=0)
{
id=id.substring(0,id.length-9);
}
//alert(id);
$("input[type='checkbox']").each(
function() {
if($(this).val().indexOf(id) >=0){
//alert($(this).val());

$(this).attr("checked",true)
}
}
);

}
});
});

</script>

附Jqury对checkbox的常用操作

jquery 遍历checkbox jquery 遍历checkbox

var a="";

$('input[type="checkbox"][name="chk"]:checked').each(

function() {

a=a+"|"+$(this).val();

}

);

$('input[type="checkbox"][name="chk"]').each(

function() {

a=a+"|"+$(this).val();

}

);

JQuery操作checkbox、radio例:将多个选中的checkbox的值组装成一个字符串

<script type=text/javascript>

function addMem(){

//var followers = document.getElementsByName("followers");

var f_str = '0';

$("input[@name='followers']").each(function(){

if($(this).attr("checked")==true){

f_str += ","+$(this).attr("value");

}

})

alert(f_str);

}

</script>

=====================

例:取选中的radio的值

var gender = $('input[@name=gender][@checked]').val();

=====================

转别人的一些东西:

jquery判断checkbox是否被选中

在html的checkbox里,选中的话会有属性checked="checked"。

如果用一个checkbox被选中,alert这个checkbox的属性"checked"的值alert($"#xxx".attr("checked")),会打印出"true",而不是"checked"!

如果没被选中,打印出的是"undefined"。觉得很奇怪是吗?继续看下去~

不要尝试去做这样的判断:if($"#xxx".attr("checked")=="true")

因为这么做是错的,jQuery的API手册上写,attr(name)的返回值是object。

所以,应该是if($"#xxx".attr("checked")==true)

====================================

jquery全选/取消选择checkbox示例:

<input type="checkbox"Double click to hide line number.">

?<script type="text/javascript">

?<!--

?$(function() {

?$("#checkedAll").click(function() {

?if ($(this).attr("checked") == true) { // 全选

? $("input[@name='checkbox_name[]']").each(function() {

? $(this).attr("checked", true);

? });

?} else { // 取消全选

? $("input[@name='checkbox_name[]']").each(function() {

? $(this).attr("checked", false);

? });

?}

?});

?});

?//-->

?</script>

=================================================

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();//清空下拉框

$(document).ready(function(){

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

if(this.checked) //this.checked或this.checked==checked;

{

alert($(this).val());

}

});

});

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","");

===========================================================以Name获得值的方法var str=""; $("input[name='newsletter']").each(function(){ if(this.checked) str+=$(this).val()+","; }) alert(str);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: