先贴一段我的代码,再细说:
//删除Select中索引值最大Option(最后一个)
4. $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
5. $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
6. $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
01
$
(
document
).
ready
(
function
()
{
02
var
checkText
;
03
checkText
=
$
(
'select#cgo_id'
).
find
(
"option:selected"
).
text
();
04
alert
(
checkText
);
05
$
(
'select#cgo_id'
).
change
(
06
function
()
{
07
checkText
=
$
(
'select#cgo_id'
).
find
(
"option:selected"
).
text
();
08
alert
(
checkText
);
09
}
10
)
11
});
02
03
04
05
06
07
08
09
10
11
var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text
var checkValue=$("#select_id").val(); //获取Select选择的Value
var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值
这两个红色的没使用过。。等我会使用了再修改日志
$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
这个和上面一样,没用过~
$("#select_id ").val(4); // 设置Select的Value值为4的项选中
$("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
1. $("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)
2. $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
3. $("#select_id option:last").remove(); 2. $("#select_id").prepend("<option value='0'>请选择</option>");
4. $("#select_id option[index='0']").remove();
5. $("#select_id option[value='3']").remove();
6. $("#select_id option[text='4']").remove();
这些都比较容易理解。 上面这几行代码参考:
http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html
下面说下最上面的代码:
$
(
document
).
ready()这是jquery的代码,让页面在载入的时候加载
$
(
'select#cgo_id'
).
change()这也是jquery的代码,让select变化的时候响应的函数。