【问题标题】:Getting how many checkboxes and radio buttons are checked获取检查了多少复选框和单选按钮
【发布时间】:2018-06-05 07:45:30
【问题描述】:

所以我的问题是,我将如何计算已选中的复选框以及答案为“是”的单选按钮...我现在用于单选按钮的代码是:

<label class="radio-inline">
<input form="ES3S" type="radio" name="Textbook'.$i.'" value="'.$Result[$i]['ID'].'"> Yes
</label>
<label class="radio-inline">
<input form="ES3S" type="radio" name="Textbook'.$i.'" value="-1">No
</label>


<span class="d-inline-block" data-toggle="popover" title="Error" data-content="This book is required by the school. If you want to cancel this out, proceed to the principals office with the book for review." data-trigger="hover">
<input form="ES3S" required checked onclick="return false;" type="checkbox" value="'.$Result[$i]['ID'].'" name="Textbook'.$i.'">
</span>

我正在考虑有一个计时器,它每秒检查一次以检查有多少复选框和单选按钮被选中,但我认为这是非常不切实际的。

SetInterval(ScriptUpdate, 1000);


function ScriptUpdate(param){


var check = $('#form').find('input[type=checkbox]:checked').length;
   alert('You have checked forms');
   return false;
}):    }

干杯!提前致谢

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 好吧,老实说,我不知道如何制作单选按钮,但是使用复选框... ^ 我在 :) 中编辑了它们
  • 既然您已经知道如何获取复选框计数,或许可以专门针对单选按钮更新您的问题?
  • 你不想按计时器来做。你想数到什么时候?也许您可以使用(自定义)事件来做到这一点。

标签: jquery html radio-button


【解决方案1】:

根据“是”文本和选中的单选按钮进行过滤:

var radioButtonSelectedCount = $('input[type=radio]:checked').parent().filter(function() {
       return $(this).text().trim()=="Yes"
    }).length;

【讨论】:

  • 效果很好。谢谢!我想知道是否有办法使它适用于某些无线电类型?谢谢
  • 是的,可以使用多种无线电类型,但为此我们需要在所有无线电输入中添加一个通用属性。例如:No 并应用以下条件: var radioButtonSelectedCount = $(document.querySelectorAll('input[filter=radio]:checked')).parent().filter( function() { return $(this).text().trim()=="Yes" }).length;
  • 我的表单中确实有共同的属性。对不起。我想说的是你应该如何在javascript中做到这一点
  • javascript 代码:var result=document.querySelectorAll('input[type=radio]:checked'); var radioButtonSelectedCount = Array.prototype.filter.call(result, function(node) { return node.parentNode.textContent.trim()=="Yes"; }).length;
【解决方案2】:

多种方法来计算选中的复选框/单选按钮的数量,这可能是最简单的:

$(document).ready(function(){
  alert($("input[type=radio]:checked").length);
});

【讨论】:

  • 我不认为它认为响应是是/否:?
【解决方案3】:

使用以下内容:-

function checkboxes(){
    var checkBoxCheckedCount = document.querySelectorAll('input[type=checkbox]:checked').length;
    var radioButtonSelectedCount = document.querySelectorAll('input[type=radio]:checked').length;
    alert(checkBoxCheckedCount+radioButtonSelectedCount);

}

然后您可以根据需要使用 checkBoxCheckedCount 和 radioButtonSelectedCount 变量。

【讨论】:

  • 这不会过滤“是”电台。
【解决方案4】:

对于单选按钮,您可以执行以下操作。

var countRadio = 0;
$('label.radio-inline').each(function(){
    if($(this).text() == "yes") {
        if($(this).children('input[type=radio]:checked').length > 0) {
            countRadio++;
        }
    }
});
console.log('Counted ' + countRadio  + ' Radiobuttons with yes');

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2011-10-18
    相关资源
    最近更新 更多