난 로렌아줌마가 이전에 표시할 수 있는 옵션을 설정할 수 있는 페이지 부하 선정됐다. 다음과 같은 코드를 사용하여 다시 찾았다.
$("#gate").val('Gateway 2');
함께
<select id="gate">
<option value='null'>- choose -</option>
<option value='gateway_1'>Gateway 1</option>
<option value='gateway_2'>Gateway 2</option>
</select>
그러나 이 작동하지 않습니다. 아무 생각 없어?
이 말해둘꼐요 노력해야 합니다. [Here& # 39 의 데모] [1]. '$ (document) 그레이디 코드에 배치한 smartupdate 붙여넣습니다':
$(function() {
$("#gate").val('gateway_2');
});
'$ (& # 39, # gate& # 39;) 스발 (& # 39, 게이트웨이형 2& # 39;) 지프로프 (& # 39, selected& # 39, (참),'
저는 jQuery 스발 () 메서드를 사용하여 발견된 큰 단점이 있다.
<select id="gate"></select>
$("#gate").val("Gateway 2");
이 경우 선택 상자 (또는 다른 입력 객체) 가 있을 때 사용되는 양식에 및 리셋 버튼 형식으로 설정된 값에 재설정하지 단추를 눌렀을 때처럼 선택해제하고 필터링되지 재설정됨 시작 값을 얻을 수 있을 것으로 기대하고 있다.
이 작동합니까 가져다줄래요 가장 좋은 것으로 보인다.
일부 상자
<select id="gate"></select>
$("#gate option[value='Gateway 2']").attr("selected", true);
텍스트 투입물
<input type="text" id="gate" />
$("#gate").attr("value", "your desired value")
Extarea 대한 투입물
<textarea id="gate"></textarea>
$("#gate").html("your desired value")
확인란 사서함의 대한
<input type="checkbox" id="gate" />
$("#gate option[value='Gateway 2']").attr("checked", true);
라디오 단추 대한
<input type="radio" id="gate" value="this"/> or <input type="radio" id="gate" value="that"/>
$("#gate[value='this']").attr("checked", true);
하지만 이제 이 질문에 대한 몇 가지 이터레이션에 그러니까말이야 doesn& # 39, 또는 기타 외부 라이브러리 및 상당히 쉽게 수행할 수 있는 jquery 필요 없다 그냥 다음과 같습니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
document.querySelector("#gate option[value='Gateway 2']").setAttribute('selected',true);
<select id="gate">
<option value='null'>- choose -</option>
<option value='Gateway 1'>Gateway 1</option>
<option value='Gateway 2'>Gateway 2</option>
</select>
끝 - < 스니핏 >;!
내 문제
get_courses(); //// To load the course list
$("#course").val(course); //// Setting default value of the list
같은 문제가 있었습니다. 다만 당시 코드에서 ajax call 을 통해 즉시 로드중 것은 내가 선택한 상자를 ajax 호출 형장의 이슬로 설정되었습니다 기본값입니다 갖고 있는 상자를 선택합니다.
이 솔루션은
get_courses();
setTimeout(function() {
$("#course").val(course);
}, 10);
물론 @icksde 및 @Korah (모두 thx!)
$(document).ready(function() {
$("#gate").val('Gateway 2');
});
그러나 시간 초과 높여줍니까 작동합니까 @icksde it& # 39 의 좋지 않을 것이라고 말했습니다. (내가) 에 대한 실제 필요한 20ms 대신 10ms). # 39 에 맞게 집 안에서 더 it& AJAX 함수를 다음과 같습니다.
$("#someObject").change(function() {
$.get("website/page.php", {parameters}, function(data) {
$("#gate").append("<option value='Gateway 2'">" + "Gateway 2" + "</option>");
$("#gate").val('Gateway 2');
}, "json");
});