2016-07-04 48 views
1

我想弄清楚哪些複選框被選中,所以我嘗試這種代碼:獲取所有的列表選中的複選框

$('.feature input[type="checkbox"').serialize(); 

這是我的HTML看起來像:

<div class="feature"> 
    <h2>Features</h2> 
    <label><input class="custom_css" checked="" type="checkbox" name="feature[]"> Custom CSS (style.css)</label> 
    <label><input class="custom_js" checked="" type="checkbox" name="feature[]"> Custom Javascript (script.js)</label> 
    <label><input class="modernizr" type="checkbox" name="feature[]"> Modernizr</label> 
    <label><input class="google_maps" type="checkbox" name="feature[]"> Google Maps</label> 
    <label><input class="custom_api" type="checkbox" name="feature[]"> Custom API</label> 
    <label><input class="font_awesome" type="checkbox" name="feature[]"> Font Awesome</label> 
</div> 

而且這是我得到的輸出:

陣列(1){[ 「var_sent_via_ajax」] =>串(67) 「特徵%5B%5D =上&功能%5B%5D =上&功能%5B%5D =上&功能%5B%5D =的」 }

現在,我怎麼知道他們已經檢查?那麼標誌是什麼意思?

+3

相關? [使用jQuery獲取選中的複選框列表](http://stackoverflow.com/q/2155622/1983854) – fedorqui

+0

您可以使用':checked'即'$('。feature input:checked')' – Satpal

回答

1

關於檢查之一:%5B%5D
答案:它們只是「[]」的原始http編碼值(序列化函數的結果)。
當服務器解析它時轉換爲[]併發送給應用程序,將其作爲數組進行威脅。

你爲什麼越來越啞:「功能%5B%5D =上&功能%5B%5D =上......」串
答:你已經忘了給每一個複選框值參數,那麼他們會像:「功能%5B%5D = custom_css &功能%5B%5D = custom_js ...」

我寫了解決方案。
以此工作示例並在服務器端應用程序上處理請求的「功能」參數,如字符串,並將其縮小爲「,」(php:$features = explode(',', $_POST['features']);

$(function() { 
 
    
 
    $('#getFeatures').click(function() { 
 
    var features = []; 
 
    $('.feature input[type="checkbox"]:checked').each(function() { 
 
     features.push($(this).val()); 
 
    }); 
 
    $('#selectedFeatures').html(features.join(',')); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="feature"> 
 
    <h2>Features</h2> 
 
    <label><input class="custom_css" checked="" type="checkbox" name="feature[]" value="custom_css"> Custom CSS (style.css)</label> 
 
    <label><input class="custom_js" checked="" type="checkbox" name="feature[]" value="custom_js"> Custom Javascript (script.js)</label> 
 
    <label><input class="modernizr" type="checkbox" name="feature[]" value="modernizr"> Modernizr</label> 
 
    <label><input class="google_maps" type="checkbox" name="feature[]" value="google_maps"> Google Maps</label> 
 
    <label><input class="custom_api" type="checkbox" name="feature[]" value="custom_api"> Custom API</label> 
 
    <label><input class="font_awesome" type="checkbox" name="feature[]" value="font_awesome"> Font Awesome</label> 
 
</div> 
 

 
<button id="getFeatures">GET FEATURES</button> 
 
<div id="selectedFeatures"></div>

+1

非常感謝你!這絕對是一個很好的答案,對我非常有幫助! – Reza

1
$(".feature input[type='checkbox']:checked").length; 

這會給你檢查複選框的數量。

$(".feature input[type='checkbox']:checked") 

這會給你選中的複選框(對象)

0

您可以通過名字來取得它們,然後通過一個

var elems= document.querySelectorAll('[name=feature[]') 
       for (var i=0;i<elems.length;i++) 
       { 
        var isChecked =elems[i].checked; 
        console.log(isChecked); 
       } 
0

你可以通過下面的例子回答:

$('input[name="feature"]:checked').each(function() { 
    console.log(this.value); 
}); 
相關問題