【问题标题】:jQuery RadioButtonList checked value returns "on"jQuery RadioButtonList 检查值返回“on”
【发布时间】:2012-06-12 19:35:36
【问题描述】:

我有一个奇怪的问题。我有一个单选按钮列表,当它被选中时,它会显示下面与所选项目相对应的 div。单选按钮列表上方是一个复选框列表。单选按钮单击事件可以正常工作,直到您从上面的复选框列表中选中一个框。从上面的复选框列表中选中一个项目后,它开始返回“on”而不是所选单选按钮的值。有什么想法吗?

jQuery:

<script type="text/javascript">
function CheckAllTrucks(sendingcb) {
    $("#divTruckList :checkbox").prop("checked", $(sendingcb).prop("checked"));
}

function SetTimeframeInput(sendingrbl) {
    var value = $(sendingrbl + ":checked").val();

    $("#divTimeFrameControls .timeframectrls").each(function () {
        $(this).hide();
    });

    $("#div" + value).show();
}

HTML/ASP.NET 代码:

<div class="form-field">
    <asp:CheckBox ID="cbSelectAllTrucks" runat="server" Text="Select All Trucks" onclick="CheckAllTrucks(this)" />
    <div id="divTruckList">
        <asp:CheckBoxList ID="cblTrucks" runat="server" />
    </div>
</div>
<div class="form-field">
    <asp:RadioButtonList ID="rblTimeFrame" runat="server" onclick="SetTimeframeInput(this)">
        <asp:ListItem Text="Month" Value="month" />
        <asp:ListItem Text="Quarter" Value="quarter" />
        <asp:ListItem Text="January-December" Value="jandec" />
        <asp:ListItem Text="July-June" Value="juljun" />
    </asp:RadioButtonList>
    <div id="divTimeFrameControls">
        <div id="divmonth" class="timeframectrls" style="display: none;">
            <!-- month fields -->
        </div>
        <div id="divquarter" class="timeframectrls" style="display: none;">
            <!-- quarter fields -->
        </div>
        <div id="divjandec" class="timeframectrls" style="display: none;">
            <!-- jandec fields -->
        </div>
        <div id="divjuljun" class="timeframectrls" style="display: none;">
            <!-- juljun fields -->
        </div>
    </div>
</div>

提前致谢!

编辑

我发现只有在选中其中一个复选框时才会发生这种情况。如果您选中然后取消选中,则该值仍然正确。只有当复选框被选中时,它才会将值设置为“on”。

【问题讨论】:

    标签: javascript jquery asp.net checkbox radio-button


    【解决方案1】:

    仍然不知道问题出在哪里,但可以通过直接引用单选按钮列表的 ID 来解决它。像这样:

    function SetTimeframeInput() {
        var value = $("#rblTimeFrame input:checked").val();
    
        $("#divTimeFrameControls .timeframectrls").each(function () {
            $(this).hide();
        });
    
        $("#div" + value).show();
    }
    

    感谢您的意见。

    【讨论】:

      【解决方案2】:

      不要将(this) 传递给函数,而是尝试使用EventObject,它在点击时默认传递:

      function SetTimeframeInput(e) {
          var value = $(e.currentTarget).val();   
      
      // value contains selected radio button`s value 
      
      // continue your code here 
      

      }

      我希望,这会有所帮助。

      【讨论】:

      • 当我做出你提到的改变时,e 是未定义的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2012-11-23
      • 2014-04-27
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多