如何使用AlloyUI获取多个复选框值

时间:2014-12-10 13:14:53

标签: yui alloy-ui

我想从提交按钮上列出的选择复选框中获取多个复选框值。我试过node-event-delegate,但它无法正常工作:

AUI().use('node', 'event', 'node-event-delegate', function (A) {
    var allcheckboxes = A.one('.plans').all(':checked');
    console.info(allcheckboxes.size()); // It will give number selected checkboxes

    A.all('.compare-products').each(function (node) {

        console.info(node.currentTarget);
        console.info(node, A.one(node(':checkbox:checked')));

        if (A.one(node.currentTarget).all(':checked')) {}
    });

    A.all('.compare-products input[type="checkbox"]').filter(':not(:checked)').setAttribute("disabled", "disabled"); // it will disable the unchecked checkboxes
    A.all('.compare-products input[type="checkbox"]').filter(':not(:checked)').removeAttribute("disabled", "disabled"); // it will remove disable attribute from unchecked checkboxes
});

1 个答案:

答案 0 :(得分:2)

HTML代码

<div>
<div>
    <input type="checkbox" class="product" name="product1" value="product1" /> product1<br />
    <input type="checkbox" class="product" name="product2" value="product2" /> product2<br />
    <input type="checkbox" class="product" name="product3" value="product3" /> product3<br />
    <input type="checkbox" class="product" name="product4" value="product4" /> product4<br />
    <input type="checkbox" class="product" name="product5" value="product5" /> product5<br />
</div>
<button>Compare Products</button></div>

AUI脚本代码

AUI().use("node", function(A){
A.one("button").on("click", function(){
    var values = [];
    A.all("input[type=checkbox]").each(function(){
        if(this.get("checked")){
            values.push(this.get("value"));
        }
    });
    console.log(values);
}); });