亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

設置選擇框的選擇選項

設置選擇框的選擇選項

浮云間 2019-11-11 14:04:46
我想設置一個先前選擇的要在頁面加載時顯示的選項。我用以下代碼嘗試了它:$("#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>但這是行不通的。有任何想法嗎?
查看完整描述

3 回答

?
慕神8447489

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

});


查看完整回答
反對 回復 2019-11-11
?
慕沐林林

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);


查看完整回答
反對 回復 2019-11-11
  • 3 回答
  • 0 關注
  • 567 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號