我想要自動選擇帶fr值的選項。使用Javascript或jQuery在select元素中選擇一個選項
<select class="goog-te-combo">
<option value="">Select Language</option>
<option value="fr">French</option>
</select>
這是可能的JavaScript或jQuery?
我想要自動選擇帶fr值的選項。使用Javascript或jQuery在select元素中選擇一個選項
<select class="goog-te-combo">
<option value="">Select Language</option>
<option value="fr">French</option>
</select>
這是可能的JavaScript或jQuery?
沒有jQuery的需要蠻力
var country="fr";
//If you know the form and name of the select:
var sel = document.forms[0].sel1;
//If you know the id of the select:
var sel = document.getElementById('sel1');
// If you only know the className:
var sels = document.getElementsByTagName('select');
for (var i=0,n=sels.length;i<n;i++) {
if (sels[i].className=="goog-te-combo") { // assume multiple selects with same class
for (var j=0, m=sels[i].options.length;j<m;j++) {
if (sels[i].options[j].value==country) sels[i].options[j].selected=true;
}
}
}
這臺法國選項:
$('.goog-te-combo').val("fr");
下面的代碼片段也將設置FR選項:
$('option[value="fr"]').attr({selected: 'true'});
你是什麼意思通過自動選擇選項?是否必須在事件後選擇它(點擊按鈕,選中複選框......),或者您只是希望它成爲默認選項?如果後者你正在尋找屬性「選擇」,如果不嘗試更具體一點,這將很容易找出;) – 2010-06-29 09:17:18