2016-12-25 134 views
0

當函數包含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塊,它的工作原理,並顯示警告。任何人都知道它爲什麼會這樣。任何幫助將不勝感激。感謝所有人提前和聖誕快樂。

+0

'begin'不是默認的$ .ajax()'選項。 '@ Url.Action(「getMakes」,「Transaction」,new {categoryId = Model.CategoryId})是否返回一個有效的URL字符串?將'type'設置爲'POST'的目的是在哪裏沒有數據發送到服務器? – guest271314

+0

我也嘗試過GET,但它不工作,我刪除了除成功之外的所有東西,仍然無法正常工作 – Sony

+0

您是否嘗試過使用字符串作爲'url'的值? – guest271314

回答

0

感謝所有那些幫助我解決這個問題的人,即使是聖誕節。最後,我通過在ajax中的url部分之前和之後放置單個逗號來實現它。感謝@AnilTalla的控制檯線索。這裏是更新的版本js

$(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"); 
       } 
      }); 
     }); 
    }); 

聖誕快樂大家。

+2

不,你應該把答案放在這裏,並留下你的問題,因爲它是 – CodingYoshi

+0

好的。我將編輯並放在這裏 – Sony

+0

謝謝,祝你有同樣的感覺@sony –

相關問題