【问题标题】:radio + label hot to get correct values (array)radio + label hot 以获得正确的值(数组)
【发布时间】:2013-02-14 20:36:51
【问题描述】:

我正在尝试从标签(收音机)中获取正确的值。当我单击标签时,我得到了值,但是当我单击另一个标签(标签)时,我从第一个标签中获取值,我需要再次单击它以将两个值添加到我的数组中。

我将隐藏单选按钮,因为我只想使用标签。我怎样才能避免这种行为?

看看:

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/

【问题讨论】:

    标签: jquery arrays label radio


    【解决方案1】:

    每次点击都会清除数组。

    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);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多