2016-06-05 4 views
0

jQuery를 사용하여 Option 요소를로드하고 있고 선택한 옵션을 설정하려고하면 작동하지 않습니다. 누군가 내가 그것을 고치기 위해 바꿀 필요가있는 것을 보여줄 수 있습니까?jQuery를 사용하여 선택한 옵션 설정

HTML

<select data-placeholder="Program View" class="chosen-select"></select> 

jQuery를

$.getJSON(path) 
.done(function (data) { 
    if (!data) { 
     return 
    } 

    setTimeout(
     function() { 
      var loadedAtLeastOneProgram = false; 

      $.each(data, function (i, val) { 
       if (val.Value.toLowerCase() != "rt") { 
        loadedAtLeastOneProgram = true; 
        $('.chosen-select').append('<option value="' + val.Value + '">' + val.Value + '</option>') 
       } 
      }) 

      $('.chosen-select').trigger('chosen:updated'); 

      if (!loadedAtLeastOneProgram) { 
       $("#programsDataItems").append("<li><a href='#'>No Tailored Programs Available</a></li>"); 
      } 

      // After loading (or reloading) the programs, see if one was previously selected. The user may have refreshed their browser. 
      // In another part of my application, I set the "CurrentProgram" 
      var program = retrieveStorageItem("CurrentProgram"); 

      if (program == null || program == undefined) { 
       program = "rt"; 
      } else { 
       // ***** This is the part that does not work. Nothing shows as selected. ****** 
       $(".chosen-select").val(program).prop('selected', true); 
      } 

     }, 50); 

}) 
.fail(function (e) { 
    // Adding logging for errors. 
}); 
+0

''에 값을 할당하지 않습니다. 당신이'program'을 설정하고 나서 val()을 설정하고 싶다면 – charlietfl

답변

0
$('.chosen-select').trigger('chosen:updated'); 

이 당신이 선택을 적용 해고해야 할 것입니다.

else { 
      // ***** This is the part that does not work. Nothing shows as selected. ****** 
      $(".chosen-select").val(program).prop('selected', true); 
      $('.chosen-select').trigger('chosen:updated'); //added 
     } 
+0

몇 주전에 옵션을로드 할 때 그 문제가 있었지만 아직 기억이 나지 않았습니다. 다시 필요할지도 모른다. 감사! – Wannabe

관련 문제