2010-03-18 5 views
1

숨겨진 필드를 기반으로 선택 상자의 값을 미리 설정하려고합니다. 사용자가 값을 다시 설정하지 않아도되는 오류가 양식에서 감지 된 후에 필요합니다. 숨겨진 필드 서버 측의 값을 설정하여이 작업을 수행합니다. 문제는 내가 선택한 값을 설정하려고 할 때 아직 선택 상자가 완료되지 않은 것 같습니다. 사람은이 문제를 해결하는 방법을 알고상자가로드되면 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> 
+0

국가가 변경되거나 페이지로드시에만 기본값을 선택해야합니까? –

답변

2

의 콜백에 사전 선택 코드를 추가 할 수 있습니다 나는이 방법을 제안 (아마도 매우 다른 방식으로?) :

function initSelect(defaultVal) { 
    $("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); 
      $("select#state option[value="+defaultVal+"]").attr('selected', 'selected'); 
     }); 
    }).change(); 
} 


$(document).ready(function(){ 
    initSelect($('#statepreset').val()); 
}); 
+0

에서만 실행되도록하고 싶습니다. 내 청소기를 다시 작성하는 것처럼 보이지만 harpax의 대답에 대한 내 의견을 참조하십시오. 나는 그것이 작은 문제라고 생각하지만, 전체적인 문제를 완전히 다른 방식으로 해결해야한다고 생각합니다. – Maarten

0

당신은해서 getJSON

<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); 
     var foo = $('#statepreset').val(); 
     $("select#state option[value="+foo+"]").attr('selected', 'selected'); 
    }); 
}); 
}); 
+0

이 방법이 효과가있는 것으로 보입니다. 그러나 이는 국가 변경시마다 호출되므로 모든 국가 변경시 숨겨진 필드의 옵션으로 상태 목록의 선택한 옵션을 다시 설정하려고 시도합니다. 나는이 코드를 사용하여 페이지로드 – Maarten

1

이에 대한 빠른 수정합니다 (해서 getJSON 콜백 내부) 콜백 내부 상태의 선택을 수정하는 코드를 넣어

,617 인
$("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); 

    // set the selected value of the state select box 
    var foo = $('#statepreset').val(); 
    $("select#state option[value="+foo+"]").attr('selected', 'selected'); 
    }); 
}); 

getJson 호출에 제공하는 콜백은 서버가 응답 할 때만 실행된다는 점을 기억하십시오.

관련 문제