0

ラベル(ラジオ)から正しい値を取得しようとしています。ラベルをクリックすると値を取得しますが、別のラベル(ラベル)をクリックすると最初のラベルから値を取得します。配列に2つの値を取得するには、もう一度クリックする必要があります。

ラベルのみを使用したいので、ラジオボタンを非表示にします。この動作を回避するにはどうすればよいですか?

見てください:

var radioChecked = $('#radioinstant :radio').is(':checked');

$('#radioinstant :radio').click(function() {
        radioChecked = !radioChecked;
    $(this).attr('checked', radioChecked);
});

var additionalProducts = new Array;

    $(".chk").click(function() {
        additionalProducts=[];
        $('.chk:checked').each(function() {
            additionalProducts.push($(this).val());
        });
        console.log(additionalProducts);
        alert(additionalProducts);
    });

http://jsfiddle.net/qfagc/

4

1 に答える 1

0

クリックするたびに配列をクリアしています。

var additionalProducts = new Array;   <--- this creates the array

    $(".chk").click(function() {
        additionalProducts=[];     <--- this clears the array on EVERY CLICK
        $('.chk:checked').each(function() {
            additionalProducts.push($(this).val());
        });
        console.log(additionalProducts);
        alert(additionalProducts);
    });
于 2013-02-28T15:25:20.397 に答える