3 回答

TA貢獻1780條經驗 獲得超1個贊
$(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貢獻2016條經驗 獲得超9個贊
我發現使用jQuery .val()方法有一個明顯的缺點。
<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")
對于文本區域輸入
<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);
- 3 回答
- 0 關注
- 567 瀏覽
相關問題推薦
添加回答
舉報