2

我想将我的多选与 ajax 绑定,但在 ajax 调用项目后没有显示。我尝试重建或刷新多选,但它仍然没有显示

 <script type="text/javascript">
     $(document).ready(function () {

         var categCheck = $('#multiselect').multiselect({
             includeSelectAllOption: true,
             enableFiltering: true
         });


         $.ajax({
             type: 'GET',
             url: "@Url.Content("~/Home/Listpositions/")",
             success: function (data) {
                 $.each(data.data, function (index, item) {
                     alert(item.PositionName);
                     var opt = $('<option />', {
                         value: item.PID,
                         text: item.PositionName
                     });
                     opt.appendTo(categCheck);

                 });
             }
         });
         $('#multiselect').multiselect('rebuild');
         categCheck.multiselect('rebuild');

         categCheck.multiselect('refresh');
         $("select.multiselect").multiselect("refresh");
   });
        </script>

这是我的多选页面链接:http ://pratikisara.com/Home/Quick 这是多选但它是空的

如何从多选 JS 文件中进行 ajax 调用(在此处输入链接描述)以及我应该在哪个代码块中进行 ajax 调用?

4

3 回答 3

3

您需要在 AJAX 成功功能中重建和/或刷新。在您的代码中,进行 AJAX 调用,然后重建多选。但重建可能发生在 AJAX 调用成功之前。尝试这个:

<script type="text/javascript">
   $(document).ready(function () {

     var categCheck = $('#multiselect').multiselect({
         includeSelectAllOption: true,
         enableFiltering: true
     });


     $.ajax({
         type: 'GET',
         url: "@Url.Content("~/Home/Listpositions/")",
         success: function (data) {
             $.each(data.data, function (index, item) {
                 alert(item.PositionName);
                 var opt = $('<option />', {
                     value: item.PID,
                     text: item.PositionName
                 });
                 opt.appendTo(categCheck);

                 // Rebuild after adding the options!
                 $('#multiselect').multiselect('rebuild');
             });
         }
     });
  });
</script>
于 2016-09-02T14:00:15.213 回答
0

通过使用重新加载它对我来说工作正常。ajax 调用中的 multiselect("reload") 函数刷新复选框列表。

var obj_ddl_ExistingIdea = $("#ddl_ExistingIdea");
        if (response.length > 0) {

            for (var i = 0; i < response.length; i++) {
                var str_newOption = '';
                str_newOption = "<option value='" + response[i].IdeabookId + "'>" + response[i].Name + "</option>";
                obj_ddl_ExistingIdea.append(str_newOption);
            }
            obj_ddl_ExistingIdea.multiselect("reload"); //using reload its working for me 
于 2017-08-09T05:47:46.033 回答
-1

这是 100% 好代码....

$(document).ready(function() {
var chkreport = $('#reporting_unit'); // Object creation for your dropdown

var ajaxpath = 'ajax/fend.php';
var ajaxUrl = baseUrl + ajaxpath;
$('#hospital_category').on('change', function() {
var hospital_id = this.value;
//alert(hospital_id);
$.ajax({
method: 'POST',
url: ajaxUrl,
data: {
perform_action: 'reporting_list',
hospital_id: hospital_id
},
dataType: 'html',
success: function(result) {

chkreport.append(result); // append option in the object created before ajax calling

// Now call multiselect function with their parameter
chkreport.multiselect({
columns: 1,
placeholder: 'Select Reporting Unit',
search: true,
selectAll: true
});

chkreport.multiselect('rebuild'); // it will reload dropdown need to create multiselect with checkbox option.
}
});

});

});
于 2017-11-07T09:23:55.717 回答