2016-07-28 1 views
0

내 페이지에 많은 항목이 모두 포함되어 있습니다. 내 페이지에 많은 select2 개의 항목이 있습니다.선택한 인덱스를 잃지 않고 데이터로 select2를 다시 채우십시오.

모달에서 새 데이터를 추가하고 모달을 닫은 후에 새 데이터로 모두 select2을 다시 채 웁니다. select2이 선택한 값을 잃어 버렸습니다.

$.get(callPath, { }, function(dataDropDown) { 

      $('.'+ refInput).each(function(){           
        $(this).html(dataDropDown); 
        $(this).trigger("change"); 
      }); 

      // $("'" + refInput + "'").html(dataDropDown); 
     }) 

나는 당신은 당신의 Ajax 요청 기능에서 선택된 값을 전달할 수 있습니다 선택된 값

답변

1
$.get(callPath, { }, function(dataDropDown) { 

     $('.'+ refInput).each(function(){ 
      // get current value: 
      var selected = $(this).select2('val');      
       $(this).html(dataDropDown); 
       $(this).trigger("change"); 
      // set it after repopulate: 
      $(this).select2('val', selected); 
    }); 
}) 
0

을 잃지 않고 요소를 다시 채워야 할 :

은 내가 사용하는 코드입니다. 이런 식으로 뭔가 :

var selectedId = $('#myselect').val(); 
$.get(callPath, { }, function(dataDropDown, selectedId) { 

     $('.'+ refInput).each(function(){           
       $(this).html(dataDropDown); 
       $(this).trigger("change"); 
     }); 
     $('#myselect').val(selectedId); 

     // $("'" + refInput + "'").html(dataDropDown); 
    }) 
+0

내 경우에는 여러 선택 2 항목 그래서 난 사실 내가 제공 한 조각은 또한이 경우에 작동해야 모두 다시 – Macnux

+0

를 반환 할 수있는 방법이있다. 선택한 값을 배열로 가져온 다음 AJAX 호출 후 값을 다시 선택해야합니다. 다음과 같은 것 : '$ ("# myselect") .val ([ "1", "2"]);' –

관련 문제