首頁 > web前端 > js教程 > 主體

jQuery分别获取选中的复选框值的示例_jquery

WBOY
發布: 2016-05-16 16:44:09
原創
1122 人瀏覽過
复制代码 代码如下:

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);
}


直接上代码,主要是获取checkbox值的方法:将其放到数组中,然后连接成字符串
复制代码 代码如下:

var chenked=$("input[type='checkbox']:checked").val([]);
var names = "";
for(var i=0;inames += 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.');
}
}
);

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{
$(":checkbox[value='"+str[j]+"']").attr("checked",true);
}
}


下拉框
复制代码 代码如下:

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

var s = $("#parentId").find("option:selected").val();
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!