2

我有一个奇怪的问题。我有一个单选按钮列表,当它被选中时,它会显示下面与所选项目相对应的 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”。

4

2 回答 2

0

不要传递(this)给函数,而是尝试使用EventObject默认情况下在 click 上传递的内容:

function SetTimeframeInput(e) {
    var value = $(e.currentTarget).val();   

// value contains selected radio button`s value 

// continue your code here 

}

我希望这有帮助。

于 2012-06-08T18:30:48.510 回答
0

仍然不知道问题是什么,但能够通过直接引用单选按钮列表的 ID 来解决它。像这样:

function SetTimeframeInput() {
    var value = $("#rblTimeFrame input:checked").val();

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

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

感谢您的输入。

于 2012-06-08T19:10:16.080 回答