基本思路:遍历select下的option,使用val()获取每个option的值,然后加入到一个数组中。根据这个思路,可以使用for循环或者jQuery的ea$("input[@type=checkbox][@checked]").val();ch()遍历函数实现。这里提供一个更简洁的代码:
jquery清空select里面的option元素 js清空select选中的值
$("option").map(function(){return
代码说明:
使用map()函数把每个option的值传递到当前匹配,生成包含返回值的对象;
使用
get()
处理返回的对象以得到基础的数组;
使用join()函数组装字符串。
实例演$("input[@type=radio][@value=2]").attr("checked",'checked');示如下:
1、HTML结构
type="button"
value="获取所有option值">
2、jquery代码
$(function(){
$(":button").click(function()
{var
=$("#test
alert(str);
jQuery获取Select元素,并设置的 Text和Value:
}}$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
$("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
jQuery添加/删除Select元素的Option项:
$("#select_id").append(""); //为Select追加一个Option(下拉项)
$("#select_id").prepend(""); //为Select插入一个Option(个位置)
$("#select_id option:last").remove(); //删除Select中索引值Option(一个)
$("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
分类
四级分类:
请选择四级分类
if($("#thirdLl").val()!=0){
$("#thirdLl option[value!=0]").remove();
}if($("#fourthLlId").val()!=0){
$("#fourthLlId option[value!=0]").remove();
}//这个表示:假如希望当选择选择第三类时:如果第四类中有数据则删除,如果没有数据第四类的商品中的为默认值。
获取Select :
获取select 选中的 text :
$("#ddlRegType").find("option:selected").text();
获取select选中的 value:
$("#ddlRegType ").val();
获取select选中的索引:
$("#ddlRegType ").get(0).selectedIndex;
$("#ddlRegType ").get(0).selectedIndex=index;//index为索引值
var
document.getElementById("mySelect");//mySelect是select
a.options[0]var select = self.find('select:eq(0)');.selected
=true;
$("#mySelect
option:first").prop("sel下拉框:ected",
'selected');
需要准备的材料分别有:电脑、html编辑器、浏览器。