利用jquery操作select下拉列表框的代码


  本文标签:jquery,select

例:
复制代码 代码如下:

<select id="sltList" name="list">
<option value="1">张三</option>
<option value="2">李四</option>
</select>
// 获取当前选中的option值
$(#sltList).val()
//获取当前选中项的文本
$(#sltList option[@selected]).text(); // 获取当前选中的option, text为文本, val则是option的值了
或 var item = $("select[@name= list] option[@selected]").text();
//设置文本为当前选中项
$("#sltList option[@selected]").attr("text",张三);

//设置select下拉框的第二个元素为当前选中值
$(#sltList)[0].selectedIndex = 1;
//设置value=1的项目为当前选中项
$("#sltList").attr("value",1);
$(#sltList).val(1);
//添加下拉框的option
$("<option value=3>王五</option><option value=4>赵六</option>").appendTo("#sltList")
//清空下拉框
$("sltList").empty();
例:
//改变时的事件
$("#testSelect").change(function(){ //事件發生
jQuery(option:selected, this).each(function(){ //印出選到多個值
alert(this.value);
});
});
$("#childTypeResult").append(htmlStr); //是在此ID标签后增加htmlStr的值.
$("#childTypeResult").html(htmlStr); 是修改此ID的值为htmlStr的值.