jquery实现动态改变下拉列表

来源:互联网 发布:java数组传到jsp 编辑:程序博客网 时间:2024/05/30 23:21

例:<select id="sltList" name="list">
<optionvalue="1">张三</option> 
<optionvalue="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的值.

原创粉丝点击