標籤:style color os java 使用 io strong for ar
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>
直接上代碼,主要是擷取checkbox值的方法:將其放到數組中,然後串連成字串
[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(‘,‘);
即可以了
//此為重點,該句與下面的第一句效果一樣
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]
java 拆分
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;
}
}
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();
jQuery分別擷取選中的複選框值