使用jQuery迭代选择框

时间:2017-11-30 13:49:40

标签: javascript jquery html jquery-selectbox

我有一个复选框列表(asp.net),每个复选框都有不同的类,表示每个值的值(1或2个点)。我的脚本遍历所有复选框并查看每个复选框的点值并返回 检查的总点数。这很好用,但我不能为选择框工作。我只想在选项='是'时计算每个点值。我不知道如何迭代它们就像查找特定值的复选框一样。感谢

<script type="text/javascript">
        $(function () {
            var total;
            var checked = $("[class*="Score"] input[type='checkbox']").click(function (e) {
                calculateScore();
            });


            function calculateScore() {
                // 1 point each
                var $checked1 = $(".Score1 :checkbox:not(:checked)")
                total = 0;
                $checked1.each(function () {
                    total += 1
                });

                // Now calculate the two pointers                    
                var $checked2 = $(".Score2 :checkbox:not(:checked)")
                $checked2.each(function () {
                    total += 2
                });

                $('#total').text("Points deducted: " + total);

            }
        });         
</script>

<!--1 point each-->
<asp:CheckBox ID="chkGName" runat="server" CssClass="Score1" />Choice 1
<asp:CheckBox ID="chkGClear" runat="server" CssClass="Score1" />Choice 2
<asp:CheckBox ID="chkGTone" runat="server" CssClass="Score1" />Choice 3

<!--2 points each-->
<asp:CheckBox ID="chkGName" runat="server" CssClass="Score2" />Choice 1
<asp:CheckBox ID="chkGClear" runat="server" CssClass="Score1" />Choice 2
<asp:CheckBox ID="chkGTone" runat="server" CssClass="Score1" />Choice 3

<!--Dropdown boxes-->

<asp:DropDownList ID="ddlScoreK1" runat="server" CssClass="Score1">
<asp:ListItem Text="" Value="" />    
<asp:ListItem Text="Yes" Value="Yes" />
<asp:ListItem Text="No" Value="No" />
<asp:ListItem Text="N/A" Value="N/A" />
</asp:DropDownList>

<asp:DropDownList ID="ddlScoreK2" runat="server" CssClass="Score2">
<asp:ListItem Text="" Value="" />    
<asp:ListItem Text="Yes" Value="Yes" />
<asp:ListItem Text="No" Value="No" />
<asp:ListItem Text="N/A" Value="N/A" />
</asp:DropDownList>

1 个答案:

答案 0 :(得分:0)

如果它可以帮助其他任何人,我就把它解决了:

<script type="text/javascript">
$(document).ready(function () {
    $('[class*="Score"]').change(function () {
        calculateScore();
  });

     function calculateScore() {
        var total = 0;
            $(".Score1").each(function () {                    
                if (this.value =='Yes') {
                    total += 1;
                }                    
            });

            $(".Score2").each(function () {
                if (this.value == 'Yes') {
                    total += 2;
                }
            });     
     }
});