jQuery分別獲取選中的復選框值


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM