jQuery分别获取选中的复选框值

[b]function jqchk(){ //jquery获取复选框值
var s='';
$('input[name="aihao"]:checked').each(function(){
s+=$(this).val()+',';
});

点击“提交”后,可以得到正确的选择值了,但是后面多一个,(英文逗号),这个可以检测一下再用substring去除,或者获取到复选框选择值后一般都要转成数组再使用的,所以也可以在转成数组后,去除最后一个数组元素。
if (s.length > 0) {
//得到选中的checkbox值序列
s = s.substring(0,s.length - 1);
}
alert(s==''?'你还没有选择任何内容!':s);
}
</script>
[/b]


直接上代码,[color=red]主要是获取checkbox值的方法:将其放到数组中,然后连接成字符串[/color]

[b]
[b]
var chenked=$("input[type='checkbox']:checked").val([]);
var names = "";
for(var i=0;i<chenked.length;i++){
names += chenked[i].value +",";
}

可以更优雅一些:

var arr_v = new Array();

=$("input[type='checkbox']:checked").each(function(){

arr_v.push(this.val());

});

arr_v.join(',');

即可以了

//此为重点[/b],该句与下面的第一句效果一样
var selectedItems = new Array();
$("input[@name='itemSelect[]']:checked").each(function() {selectedItems.push($(this).val());});

if (selectedItems .length == 0)
alert("Please select item(s) to delete.");
else
$.ajax({
type: "POST",
url: "/ajax_do_something.php",
data: "items=" + selectedItems.join('|'),
dataType: "text",
success: function (request) {
document.location.reload();
},
error: function(request,error){
alert('Error deleting item(s), try again later.');
}
}
);[/b]

[b]java 拆分[/b]
[b]String names = null;
String name1 = null;
String name2 = null;
names = request.getParameter("names");
String[] name = names.split(",");
for(String x : name){
if("zhangsan".equals(x)){
name1 = x;
}
if("lisi".equals(x)){
name2 = x;
}
}[/b]


jquery 修改时候选中 后台查询的复选框

var struids='${useridstr}'; //后台获取数据
alert(struids);
if(struids!='')
{
var str=struids.split(",");
for(var j=0;j<str.length;j++)
{
$(":checkbox[value='"+str[j]+"']").attr("checked",true);
}
}

//复选框


下拉框
var module='${module}'
$("#module option[value='" + module + "']").attr("selected","selected");

var s = $("#parentId").find("option:selected").val();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值