【问题标题】:Iterating through select boxes with jQuery使用 jQuery 遍历选择框
【发布时间】:2018-05-14 11:26:08
【问题描述】:

我有一个复选框列表(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>

【问题讨论】:

  • 你能分享渲染的html而不是asp:吗?
  • 选择框渲染如下
  • 如何知道哪个选择框映射到哪个复选框?每个复选框是否有多个选择框?
  • 仅在 html 中提供 minimal reproducible example
  • 它们根本没有相互映射。我只是使用复选框作为我想要完成但使用选择框的示例。

标签: javascript jquery html jquery-selectbox


【解决方案1】:

如果它对其他人有帮助,我会解决:

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

【讨论】:

    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多