关键代码:
<select name="star" id="star" data-toggle="selectpicker" onchange="getDiscount()">1.动态添加option项: 在后面添加:
$("#star").append("<option value='"+value+"'>"+text+"</option>");在前面添加:
$("#star").PRepend("<option value='"+value+"'>"+text+"</option>");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; } }示例 动态获取select控件的option:
新闻热点
疑难解答