숨겨진 필드를 기반으로 선택 상자의 값을 미리 설정하려고합니다. 사용자가 값을 다시 설정하지 않아도되는 오류가 양식에서 감지 된 후에 필요합니다. 숨겨진 필드 서버 측의 값을 설정하여이 작업을 수행합니다. 문제는 내가 선택한 값을 설정하려고 할 때 아직 선택 상자가 완료되지 않은 것 같습니다. 사람은이 문제를 해결하는 방법을 알고상자가로드되면 jQuery는 옵션 상자에서 선택된 값을 설정합니다.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
// this functions loads the state select box with states if a country with states is selected
$("select#countries").change(function(){
$.getJSON("/ajax/exhibition/getstates.php?idCountry="+$("select#countries").val(), function(j){
var options = '';
$.each(j, function(key, value){
options += '<option value="' + key + '">' + value + '</option>';
});
$("select#state").html(options);
});
});
});
$(document).ready(function(){
// preset the state select box on page ready
$('select#countries').change();
// set the selected value of the state select box
var foo = $('#statepreset').val();
$("select#state option[value="+foo+"]").attr('selected', 'selected');
});
</script>
국가가 변경되거나 페이지로드시에만 기본값을 선택해야합니까? –