關(guān)鍵代碼:
<select name="star" id="star" data-toggle="selectpicker" onchange="getDiscount()">1.動(dòng)態(tài)添加option項(xiàng): 在后面添加:
$("#star").append("<option value='"+value+"'>"+text+"</option>");在前面添加:
$("#star").2.刪除選中的option項(xiàng):$("#star option:selected").remove();3.清空select下拉框:
方式一:
$("#star option").remove();方式二:
$("#star").empty();4.獲取Select選中項(xiàng)的text:
$("#star").find("option:selected").text();5.獲取Select選中項(xiàng)的value: 方式一:
$("#star option:selected").attr("value");方式二:
$("#star").val();6.獲取Select選中項(xiàng)的索引值,從0開始: 方式一:
$("#star option:selected").index();方式二:
$("#star").get(0).selectedIndex;7.獲取Select最大索引值,從0開始:
$("#star option:last").index();8.設(shè)置選中項(xiàng): 根據(jù)索引設(shè)置選中項(xiàng):
$("#star").get(0).selectedIndex=index;//index為索引值根據(jù)value設(shè)置選中項(xiàng): 方式一:
$("#star").attr("value","newValue");方式二:
$("#star").val("newValue");方式三:
$("#star").get(0).value = value;根據(jù)text設(shè)置對(duì)應(yīng)的項(xiàng)為選中項(xiàng):
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; } }示例 動(dòng)態(tài)獲取select控件的option:
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注