当前位置: 首页 > Web前端 > HTML

复选框

时间:2023-04-02 21:09:49 HTML

[1]批量增删时如何判断是否至少选中一条数据$("#add").click(function(){//批量使用(获取选中的复选框)vartest=$("input[name='btSelectItem']:checked");varcheckBoxValue="";test.each(function(){checkBoxValue+=$(this).val()+",",";})checkBoxValue=checkBoxValue。substring(0,checkBoxValue.length-1);//todo验证varrows=$('#exampleTable').bootstrapTable('getSelections');//返回所有选中的行,当没有选中的记录时,返回一个空arrayif(rows.length==0){toastr.error("请选择至少一个供应商");return;}else{vartypeid=$("#typeid").val();$.ajax({缓存:true,类型:“POST”,url:“/base/supSupt/save?typeid="+typeid+"&ids="+encodeURIComponent(checkBoxValue),数据:null,异步:true,错误:函数(请求){toastr.error("Connectionerror");},success:function(data){if(data.code==0){toastr.success("操作成功。");$('#modal-addSup').modal('隐藏');//刷新更新细节();窗口打开();}else{toastr.error(data.msg);}}});}})[2]表格选择下面官方的bootstrap写法$("#add").click(function(){varrows=$('#exampleTable').bootstrapTable('getSelections');//返回所有选中的行,当没有选中的记录时,返回一个空数组if(rows.length==0){toastr.error("请选择要添加的供应商");return;}else{varids=newArray();$.each(rows,function(i,row){ids[i]=row['id'];});console.log(ids);$.ajax({type:'POST',数据:{“ids”:ids},url:'/base/supSupt/save?typeid='+$(“#typeid”).val(),成功:函数(r){if(r.code==0){toastr.success(r.msg);window.open();}else{toastr.error(r.msg);}}});}})代码隐藏:/***Save*/@ResponseBody@PostMapping("/save")@RequiresPermissions("base:supSupt:add")publicRsave(SupSuptDOsupSupt,Modelmodel,@RequestParam("typeid")Stringtypeid,@RequestParam("ids[]")String[]ids){//将要获取的ids(供应商的id=supid)用逗号拆分成数组inti=0;for(Stringsupid:ids){i++;Stringuuid=UUID.randomUUID().toString().replace("-","");supSupt.setToid(uuid);supSupt.setTypeid(typeid);supSupt.setSupid(supid);if(supSuptService.save(supSupt)>0){if(ids.length==i){returnR.ok();}else{继续;}}else{返回R.error();}}returnR.error();}【3】全选复选框,禁止全选//html:<inputtype="checkbox"class="group-checkable"data-set="#sample_1.checkboxes"/>js脚本:$(".group-checkable").click(function(event){$(this).toggleClass("checkboxes");varisChecked=$('.group-checkable').prop('checked');if(isChecked){$('input[name="ids"]').prop("checked",true);}else{$('input[name="ids"]').prop("checked",false);}});==tips==:因为这个复选框是要提交的,所以需要在form中添加from表单,然后$("#formid").serialize();自然会转入后台获取对应的id数组[4]注意:数组可以放在FromData中;您可以将数组放在路线中;也可以在dto中声明数组变量,然后页面就是数组名varbidfiletypeids=newArray();//定义一个数组bidfiletypeids=checkBoxValue.split(",");//字符拆分