轉載:https://blog.csdn.net/huakaiba/article/details/78026430
使用 jquery 獲取一組或者單個 checkbox 的選中狀態的值。下面通過一個示例進行說明,
假設現有一頁面有一組 checkbox 的 name 的值為 id ,那么獲取這組 name=id 的checkbox的值的方法如下:
Js代碼:
var id_array=new Array(); $('input[name="id"]:checked').each(function(){ id_array.push($(this).val());//向數組中添加元素 }); var idstr=id_array.join(',');//將數組元素連接起來以構建一個字符串 alert(idstr);
另外 jquery 判斷單個 checkbox 是否選中以及獲取選中值的方法如下:
if($("#id").is(":checked")){//選中 alert($("#id").val());//打印選中的值 }
我自己的實現全選和刪除所選:
<script type="text/javascript"> $(function(){ $("#select").click(function(){ //獲取下面所有的 復選框並將其選中狀態設置跟編碼的前端 復選框保持一致。 //attr方法與JQ的版本有關,在1.8.3及以下有效。 //$("tbody input").attr("checked",this.checked); $("tbody input").prop("checked",this.checked); }); }); function delSelect(){ var isDel = confirm("您確認要刪除嗎?"); var pid_array = new Array(); if(isDel){ //要刪除所有選項 $('input[id="selectone"]:checked').each(function(){ pid_array.push($(this).val());//向數組中添加元素 }); var pids=pid_array.join(","); location.href = "${pageContext.request.contextPath}/adminDelProduct?pids="+pids; } } </script>
作者:geeooooz
鏈接:https://www.jianshu.com/p/eec33c5aa9f1
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。