3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
$(document).ready(function() {
$("#gate option[value='Gateway 2']").prop('selected', true);
// you need to specify id of combo to set right combo, if more than one combo
});

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
我發(fā)現(xiàn)使用jQuery .val()方法有一個(gè)明顯的缺點(diǎn)。
<select id="gate"></select>
$("#gate").val("Gateway 2");
如果此選擇框(或任何其他輸入對(duì)象)在表單中,并且表單中使用了重置按鈕,則單擊重置按鈕時(shí),設(shè)置值將被清除,并且不會(huì)重置為您期望的初始值。
這似乎最適合我。
對(duì)于選擇框
<select id="gate"></select>
$("#gate option[value='Gateway 2']").attr("selected", true);
對(duì)于文字輸入
<input type="text" id="gate" />
$("#gate").attr("value", "your desired value")
對(duì)于文本區(qū)域輸入
<textarea id="gate"></textarea>
$("#gate").html("your desired value")
對(duì)于復(fù)選框
<input type="checkbox" id="gate" />
$("#gate option[value='Gateway 2']").attr("checked", true);
對(duì)于單選按鈕
<input type="radio" id="gate" value="this"/> or <input type="radio" id="gate" value="that"/>
$("#gate[value='this']").attr("checked", true);
添加回答
舉報(bào)