jQuery分別擷取選中的複選框值

來源:互聯網
上載者:User

標籤: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分別擷取選中的複選框值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.