我有一个MVC应用程序,可以对所有选择的所有日期进行简单控制或选择日期范围。
单选按钮有一个onclick处理程序,用于启用/禁用数据选择器。到目前为止一切顺利。
当我尝试在执行POST后为datepickers设置正确的上下文状态时,我无法让jQuery选择器返回选中的单选按钮值。
代码如下:
<%= Html.RadioButton("DateSelection.AllDates", "AllDates", Model.AllDates == "AllDates", new { onclick = "setReadOnly(this);" })%>ALL Dates
<%= Html.RadioButton("DateSelection.AllDates", "Selection", Model.AllDates == "Selection", new { onclick = "setReadOnly(this);" })%>Selection
<%= Html.DatePicker("DateSelection.startdate", Model.StartDate, "", "") %>
To <%= Html.DatePicker("DateSelection.enddate", Model.EndDate, "", "") %><br />
javascript如下:
<script type="text/jscript" >
function setReadOnly(obj) {
if (obj.value == "Selection") {
$('#DateSelection_startdate').css('backgroundColor', '#ffffff')
.removeAttr('readonly')
.datepicker('enable');
$('#DateSelection_enddate').css('backgroundColor', '#ffffff')
.removeAttr('readonly')
.datepicker('enable');
}
else {
$('#DateSelection_startdate').css('backgroundColor', '#eeeeee')
.attr('readonly', 'readonly')
.val('')
.datepicker('disable');
$('#DateSelection_enddate').css('backgroundColor', '#eeeeee')
.attr('readonly', 'readonly')
.val('')
.datepicker('disable');
}
}
<script type="text/jscript">
$(document).ready(function() {
$('#DateSelection_startdate').datepicker('disable').css('backgroundColor', '#eeeeee')
.attr('readonly', 'readonly')
.val('');
$('#DateSelection_enddate').datepicker('disable').css('backgroundColor', '#eeeeee')
.attr('readonly', 'readonly')
.val('');
var selected = $('#DateSelection_AllDates:checked');
setReadOnly(selected);
});
导致问题的javascript行是
var selected = $('#DateSelection_AllDates:checked');
不会返回选中的单选按钮。
使用
var selected = $('#DateSelection_AllDates');
将按预期返回第一个单选按钮值,即应用':checked'过滤器ALWAYS返回undefined。
这里有人能看到什么问题吗?
答案 0 :(得分:1)
我认为你需要更像这样的东西:
$("input[name='DateSelection_AllDates']:checked")
很难说没有看到你生成的HTML,因为我不知道MVC。很明显,你有多个具有相同NAME的单选按钮(因此是一个组,是吗?)。这与ID不同,根据定义,ID必须是唯一的。