<select disabled="disabled" name="typename" class="form-field col-sm-12 selecttype ">
<option value="2">重點工做</option>
<option value="1">OKR</option>
</select>
jquery
// select 設置選中值
$(".selecttype").val(data.type + "");app
能夠參考 form 的 操做select 代碼:this
form.find('select').each(function() {
var name = $(this).attr('name');
try {
if (name == p && name != breakFields) {
var value = result[p].toString();
if (isNotEmpty(value)) {
//console.log(name+" select value:"+value);
if (value.indexOf(",") != -1) {
var vals = value.split(",");
for (var dex = 0; dex < vals.length; dex++) {
//console.log(" select dex:"+dex+",val:"+vals[dex]);
$(this).find("option[value=" + vals[dex] + "]").prop('selected', true);
}
} else {
$(this).find("option[value=" + value + "]").prop('selected', true);
}
}
isContinue = true;
return false;
}
} catch (e) {
console.log(e);
}
});.net
參考地址:orm
http://blog.csdn.net/nairuohe/article/details/6307367blog
每一次操做select的時候,老是要出來翻一下資料,不如本身總結一下,之後就翻這裏了。get
好比<select class="selector"></select>it
一、設置value爲pxx的項選中io
$(".selector").val("pxx");console
二、設置text爲pxx的項選中
$(".selector").find("option[text='pxx']").attr("selected",true);
這裏有一箇中括號的用法,中括號裏的等號的前面是屬性名稱,不用加引號。不少時候,中括號的運用能夠使得邏輯變得很簡單。
三、獲取當前選中項的value
$(".selector").val();
四、獲取當前選中項的text
$(".selector").find("option:selected").text();
這裏用到了冒號,掌握它的用法並觸類旁通也會讓代碼變得簡潔。
不少時候用到select的級聯,即第二個select的值隨着第一個select選中的值變化。這在jQuery中是很是簡單的。
如:$(".selector1").change(function(){
// 先清空第二個
$(".selector2").empty();
// 實際的應用中,這裏的option通常都是用循環生成多個了
var option = $("<option>").val(1).text("pxx");
$(".selector2").append(option);
});