当函数包含ajax调用时,下拉选择更改不会触发。如果该函数只包含一个警报,则该函数起作用。这里是代码Ajax调用选择更改未触发
<div class="col-md-6">
<div class="form-group">
<label>Select Category</label>
@Html.DropDownListFor(x => x.CategoryId, new SelectList(Model.Categories, "CategoryId", "CategoryName", Model.CategoryId),
new { @class = "form-control select2", id = "myCategories" })
</div>
<div class="form-group" id="makes">
</div>
</div>
这里是js函数
@section scripts{
<script type="text/javascript">
$(document).ready(function() {
$(document.body).on('change', "#myCategories", function (event) {
var selected = $('#myCategories').val();
alert("catid changed to : " + selected);
$.ajax({
url : @Url.Action("getMakes","Transaction", new {categoryId= Model.CategoryId}),
success: function (data) {
alert(data);
$('#makes').html = data;
},
error: function() {
alert("failed");
}
});
});
});
</script>
}
如果我评论了ajax块,它的工作原理,并显示警告。任何人都知道它为什么会这样。任何帮助将不胜感激。感谢所有人提前和圣诞快乐。
'begin'不是默认的$ .ajax()'选项。 '@ Url.Action(“getMakes”,“Transaction”,new {categoryId = Model.CategoryId})是否返回一个有效的URL字符串?将'type'设置为'POST'的目的是在哪里没有数据发送到服务器? – guest271314
我也尝试过GET,但它不工作,我删除了除成功之外的所有东西,仍然无法正常工作 – Sony
您是否尝试过使用字符串作为'url'的值? – guest271314