我有一个调用 jquery 函数的 @Html.DropDownList。但它未能调用该函数。 我试过了-
$(document).ready(function () {
$('.test').change(function () {
alert("1");
});
});
@Html.DropDownList("PropertyContent", ViewBag.PropertyList as List<SelectListItem>, new { @class="test"})
和
function test() {
alert("1");
}
@Html.DropDownList("PropertyContent", ViewBag.PropertyList as List<SelectListItem>, new { onchange="test();"})
@Scripts.Render("~/bundles/jqueryval") is also included in the view
这里有什么问题吗?
最佳答案
您的 jQuery 选择器有误,请改用 id-selector:
$(document).ready(function () {
$('#PropertyContent').change(function () {
alert("1");
});
});
还有一些可能的错误:
- 页面中未引用 jQuery。要检查这一点,请打开 firebug、chrome dev tools 或 IE dev tools 并检查是否有任何错误
- 一个常见的错误是在 View 中包含 $(document).ready,并且只在底部引用 jQuery。这将产生一个错误,因为此时
$
是未知的。可能的解决方案:- 将代码移动到外部文件并在包含 jQuery 后引用该文件
- 将您的 jQuery 声明移到标题部分(不推荐,因为它会减慢页面加载速度)
关于javascript - MVC4 View 未检测到 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16561513/