js判断复选框,是单选还是多选,并且执行相应的事件
项目中有这个需求:删除可以多选,修改只能单选。
以下是某公司后台管理系统截图
1,修改操作:
// 修改 $("body").on('click','#xiugai',function () { if ($("input[name='sex']:checked").length == 0) { alert('请选择一条记录再进行修改!'); return; } if ($("input[name='sex']:checked").length > 1) { alert('只能选择一条记录进行修改!'); return; } // $(this).attr("data-href","golfTour_edit.html") window.location.href="golfTour_edit.html"; // 获取当前父元素下面的 姓名 年龄 等字段 var arr = $("input[name='sex']:checked"); var newid = arr.parents("tr").attr('id'); // var text1=$(this).parents('tr').attr('id'); //构建缓存对象 var sendNextPage={ golfTourId:newid } // //把缓存对象编程json字符串 sendNextPage=JSON.stringify(sendNextPage); //进行本地缓存---------这句话之后 本地就有这个信息了 window.localStorage.setItem('sendNextPage',sendNextPage);
2,删除操作
// 删除 $("body").on("click","#del",function(){ if ($("input[name='sex']:checked").length == 0){ alert("请至少选择一个赛事进行删除!"); return false; } var empIds=""; //用来存放父元素ID的 //遍历被选中的复选框 $.each($("input[name='sex']:checked"),function () { // empNames+=$(this).parents("tr").attr("id")+","; empIds+=$(this).parents("tr").attr("id")+","; }); empIds=empIds.substring(0,empIds.length-1); layer.open({ title: '删除数据', content: '确定删除吗?', btn: ['确定'], yes: function(index, layero) { $.ajax({ url: hosts + '/rest/web/golfTourManage/deleteGolfTourById', type: "get", data: "tourId=" + empIds + "&token=" + obj, //把父元素ID带给后台 // dataType:"json", success: function (data) { status(data); if (data.status == 200) { window.location.href = 'golfTour.html' } } }) } }); })