我有一个奇怪的问题。我有一个单选按钮列表,当选中它时,它会显示下面与所选项目相对应的 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”。
最佳答案
仍然不知道问题出在哪里,但可以通过直接引用单选按钮列表的 ID 来修复它。像这样:
function SetTimeframeInput() {
var value = $("#rblTimeFrame input:checked").val();
$("#divTimeFrameControls .timeframectrls").each(function () {
$(this).hide();
});
$("#div" + value).show();
}
感谢您的意见。
关于javascript - jQuery RadioButtonList 检查值返回 "on",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10953986/