我想自动选择值为fr。
的选项<select class="goog-te-combo">
<option value="">Select Language</option>
<option value="fr">French</option>
</select>
这可以使用javascript或jQuery吗?
答案 0 :(得分:4)
这设置了法语选项:
$('.goog-te-combo').val("fr");
答案 1 :(得分:1)
没有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;
}
}
}
答案 2 :(得分:1)
以下代码段还将设置fr选项:
$('option[value="fr"]').attr({selected: 'true'});