【发布时间】:2020-04-08 10:28:08
【问题描述】:
如果用户在下拉列表 (#status) 中选择状态 2,则尝试使用当前日期更新 datepicker(#test2)。不知道我的错误是什么,感谢任何帮助。
<div class="form-group">
@Html.LabelFor(model => model.Rental.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(m => m.Rental.Status, "Select Status", new { @class = "form-control", id = "status" })
@Html.ValidationMessageFor(model => model.Rental.Status, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReturnDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(x => x.Rental.ReturnDate, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date", id = "test2" })
@Html.ValidationMessageFor(model => model.Rental.ReturnDate, "", new { @class = "text-danger" })
</div>
</div>
<script>
$(document).ready(function () {
$("#status").change(function () {
var selectedVal = $(this).children("option:selected").val();
// console.log(selectedVal);
if (selectedVal == 2) {
// console.log(selectedVal);
$("#test2").datepicker({ dateformat: "0:yyyy-MM-dd" }).datepicker('setDate', new Date());
}
});
});
</script>
【问题讨论】:
-
尝试使用
$(this).val();而不是$(this).children("option:selected").val();。 jQuery.val() documentation -
您使用的是纯 HTML 日期控件还是 JS 日期选择器库?如果是后者,你用的是哪一个?
-
感谢您的回复@Robiseb。我能够获得 selectedVal 值并进入 if 语句,它是关于更新不起作用的日期选择器的部分。
-
@RoryMcCrossan 我正在使用 jquery ui
标签: javascript jquery model-view-controller