關鍵代碼:
<select name="star" id="star" data-toggle="selectpicker" onchange="getDiscount()">1.動態(tài)添加option項: 在后面添加:
$("#star").append("<option value='"+value+"'>"+text+"</option>");在前面添加:
$("#star").2.刪除選中的option項:$("#star option:selected").remove();3.清空select下拉框:
方式一:
$("#star option").remove();方式二:
$("#star").empty();4.獲取Select選中項的text:
$("#star").find("option:selected").text();5.獲取Select選中項的value: 方式一:
$("#star option:selected").attr("value");方式二:
$("#star").val();6.獲取Select選中項的索引值,從0開始: 方式一:
$("#star option:selected").index();方式二:
$("#star").get(0).selectedIndex;7.獲取Select最大索引值,從0開始:
$("#star option:last").index();8.設置選中項: 根據索引設置選中項:
$("#star").get(0).selectedIndex=index;//index為索引值根據value設置選中項: 方式一:
$("#star").attr("value","newValue");方式二:
$("#star").val("newValue");方式三:
$("#star").get(0).value = value;根據text設置對應的項為選中項:
var count=$("#star option").length; for(var i=0;i<count;i++) { if($("#star").get(0).options[i].text == text) { $("#star").get(0).options[i].selected = true; break; } }示例 動態(tài)獲取select控件的option:
新聞熱點
疑難解答