當函數包含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