我正在尝试在@Html.TextBox上插入一个日期选择器。日期字段将用作搜索条件字段,以便我可以将日期条目与表中的日期进行比较。这是我为我的脚本准备的:
<link href="~/Content/ui_1.10.4_themes_smoothness_jquery-ui.css" rel="stylesheet" />
<script src=" ~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#getdate").each(function () {
$(this).datepicker();
});
});
这是我为我的TextBox准备的:
Date Received: @Html.TextBox("SearchString5", new {@class="getdate"}, ViewBag.CurrentFilter as string)它的结果是单词new {@class="getdate"}出现在TextBox中。
发布于 2016-06-30 03:28:59
整个代码看起来有buggy,首先您添加了一个getdate类,并且在jquery中使用了id选择器。
$(document).ready(function () {
$("#getdate").each(function () {
$(this).datepicker();
});
});这应该是
$(document).ready(function () {
$(".getdate").each(function () {
$(this).datepicker();
});
});第二件事是在helper中缺少值参数,应该是这样的
Date Received: @Html.TextBox("SearchString5",ViewBag.CurrentFilter as string, new {@class="getdate"})发布于 2016-06-30 03:29:05
这是因为您的参数对于Html.TextBox的重载方法来说是混乱的。
它们应该是这样的:
public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper,
string name,
object value,
string format,
object htmlAttributes
)因此,对于您的特定案例:
@Html.TextBox("SearchString5",ViewBag.CurrentFilter as string, new {@class="getdate"})此外,在您的JS中,您将使用#引用ID。相反,您需要引用具有.的class。
$(document).ready(function () {
$(".getdate").each(function () {
$(this).datepicker();
});
});发布于 2016-06-30 03:27:48
您的参数顺序不正确。它应该是这样的,首先是元素名称,然后是值,然后是html属性。这样写吧:
@Html.TextBox("SearchString5",ViewBag.CurrentFilter as string, new {@class="getdate"})现在在jquery中使用类选择器,因为id对于html的每个元素都应该是唯一的:
$(document).ready(function () {
$(".getdate").each(function () {
$(this).datepicker();
});https://stackoverflow.com/questions/38108369
复制相似问题