Jquery中的checkbox 及radio的问题
在web开发中,我们经常会对checkbox和radio进行读写操作,下面我来分享一下我的项目中的相关案例:
一、checkbox
<input id="check1" class="othercheck" type="checkbox" name="othercheck" value="1">1
1.判断单个已知checkbox是否选中
var ischeck=$("#check1").is(':checked');//获取此checkbox是否选中,被选中则 ischeck=true 反之为 false
2.判断多个name=test的checkbox是否选中
function judgechecked(){
var obj=document.getElementsByName('test'); //选择所有name="'test'"的对象,返回数组
//取到对象数组后,我们来循环检测它是不是被选中
var s='';
for(var i=0; i<obj.length; i++){
if(obj[i].checked)
s+=obj[i].value+','; //如果选中,将value添加到变量s中
}
//那么现在来检测s的值就知道选中的复选框的值了
alert(s==''?'你还没有选择任何内容!':s); //条件运算符
}
3.//jquery获取复选框值
function getcheckval(){
var chk_value =[];
$('input[name="test"]:checked').each(function(){
chk_value.push($(this).val()); //
});
alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);
}
或者获得拼接字符串
function getcheckval(){
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+",";
})
alert(str);
})
4.全选
1)点击button全选
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');//全选
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//取消全选
})
2)选中checkbox全选及取消
<input id="checkAll" type="checkbox" onclick="checkAll('checkbox')" name="checkAll">//选中触发checkAll()函数,选中所有name为checkbox的复选框
function checkAll(checkname){
var checkid = document.getElementById("checkAll")
var check = document.getElementsByName(Obj)
if (checkid.checked){
$("[name='checkname']").attr("checked",'true');//全选
}else{
$("[name='checkname']").removeAttr("checked");//取消全选
}
}
5.给checkbox赋值
$('input:checkbox').eq(索引值).attr('checked','true');//索引值=0,1,2
$('input:checkbox').slice(0,2).attr('checked','true');//同时选中第一个和第三个checkbox
$('input:checkbox:first').attr('checked','checked');//设置第一个checkbox为选中值
$('input:checkbox:last').attr('checked','checked');//设置第一个checkbox为选中值
$('input:checkbox[value='1']').attr('checked','true');//根据value值设置checkbox为选中
$("input").attr("checked","checked");
$("input").attr("checked",true);
6.删除操作
$('input:checkbox[value='1']').remove();//删除value=1的checkbox
$('input:checkbox').eq(索引值).remove();//索引值=0,1,2,删除第几个checkbox
二、radio
<input id="radiono" type="radio" value="1" name="isused">
var isused=$('input:radio[name="isused"]:checked').val();//获取radio的value值
分组: 只要name一样,就是一组的,即一组中只能选择一个
<input type="radio" id="radio1" checked="checked" name="group1" />radio1
<input type="radio" id="radio2" name="group1" />radio2
<input type="radio" id="radio3" name="group1" />radio3
var group1 = $("[name='group1']").filter(":checked");
alert(group1.attr("id"));
根据id选中radio
$("#radio2").attr("checked", "checked");
根据id取消选中radio
$("#radio1").removeAttr("checked");
一组中某一个被选中触发函数
$("[name='group1']").on("change",
function (e) {
console.log($(e.target).val()); //得到选中值的value
}
);
任何元素都可以有点击事件onclick,在onclick中给函数传值 如
<input id="57*1" type="radio" onclick="chkRadio(this)" value="5" name="radio1">
var flag = true;
function chkRadio(checkedradio) {//点击选中,再点击取消选中
checkedradio.checked = flag;
flag = !flag;
}
<img id="tttxx" onclick="isShowtheHidden(this.id);" src="/webStatic/image/back/hfk.png" style="margin-top: -20px;float:right;">
function isShowtheHidden(id){
id为被选中radio的id值
}
Jquery中的checkbox 及radio的问题的更多相关文章
- jquery中选择checkbox拼接成字符串,然后到后台拆分取值
jquery中选择checkbox拼接成字符串,然后到后台拆分取值 js中的代码 $("#btn").click(function(){ var chenked=$("i ...
- Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码
随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox. Radiobutton . DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的 ...
- js jquery中判断checkbox是否被选中的方法
在js中: document.getElementById("checkboxID").checked 返回true或者false jQuery中: $("input ...
- 二十四、小程序中改变checkbox和radio的样式
来源:https://blog.csdn.net/qq_39364032/article/details/79742415 在微信小程序里面,有时候为了配合整个项目的风格,checkbox和radio ...
- jQuery中的checkbox问题
一开始的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Microsoft.Office.Interop.Excel 读取 excel 中的 checkbox 和 radio
using Excel = Microsoft.Office.Interop.Excel; Excel.Application excelapp = new Excel.Application(); ...
- Jquery中对checkbox的各种“全选”或者“取消”功能实现(特别注意1.6+的一定不能使用attr来取属性了!用prop!)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Jquery 中的CheckBox、 RadioButton、 DropDownList的取值赋值
1.获取选中值,三种方法都可以: $('input:radio:checked').val(): $("input[type='radio']:checked").val(); $ ...
- Jquery中input:type=radio的监听,获取设置值
一.html <div id='demo'> <input type='radio' name='sex' value='男' > <input type='radio' ...
随机推荐
- mysql - join two derived tables
select t1.uid from (select uid from table1) t1 inner join (select uid from table2) t2 where t1.uid=t ...
- Struts框架——(二)Struts原理with登录实例
二. Struts基本工作流程 假设现在有以下情景: 用户正在浏览一个用STRUTS的技术构建的网站主页,主页上有个登陆表单,用户填好登陆名和密码,单击"登陆"按钮,就激活了以下一 ...
- myeclipse中如何导入mysql-connector-java-5.1.8-bin.jar【环境配置和工具使用】
前提:我建立了一个java project,工程名字为Test,现在需要连接mysql数据库,所以提前从网上将java操作mysql数据库的mysql-connector-java-5.1.8-bin ...
- WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用
WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用
- NGUI界面动画
玩游戏的时候,点击一个按钮,可能会看到UI从某个位置飞进来,关闭之后又往该位置飞出!又或者一些更加复杂的运动轨迹. 我们的项目现在就是使用Animation/Animator来制作界面动画. 流程:由 ...
- [刘阳Java]_MyBatis_映射文件的select标签入门_第3讲
1.Mybatis映射文件的<select>标签主要帮助我们完成SQL语句查询功能,<select>标签它包含了很多属性,下面简单对<select>标签的属性做一个 ...
- Quartz.net 定时调度CronTrigger时间配置格式说明
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- ng2收获
1.devDependencies下只有在开发应用时才用得到这个我是知道的. 但是我不知道的事要想达到这个效果是要在生产环境安装包的时候必须要加个这个才行"--production" ...
- MacDev.Mach-O.Programming-Part-III:MachOView-v2.4.9200.dmg-crash
MachOView-v2.4.9200.dmg Crash 在OS X(其版本号: 10.11.6 (15G31))下载MachOView-2.4.9200.dmg后,打开Fat Binary后,Ma ...
- C++ STL 助记1:vector
vector<, ); // Creates vector of 10 ints with value 100 vector<, "hello"); vector< ...