有關select的取值或賦值方式:
獲取select被選中項的文本
var item = $("select[@name= stsoft] option[@selected]").text();
select下拉框的第二個元素爲當前選中值
$('#stsoft')[0].selectedIndex = 1;
獲取value值
$('#stsoft').val();
設置value=1的項目爲當前選中項
$("#stsoft").attr("value",「1」);
$('#stsoft').val(「1」);
--------------------------------------------------------------------
//獲取第一個option的值
$('#test option:first').val();
//最後一個option的值
$('#test option:last').val();
//獲取第二個option的值
$('#test option:eq(1)').val();
//獲取選中的值
$('#test').val();
$('#test option:selected').val();
//設置值爲2的option爲選中狀態
$('#test').attr('value','2');
//設置第一個option爲選中
$('#test option:last').attr('selected','selected');
$("#test").attr('value' , $('#test option:last').val());
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());
//獲取select的長度
$('#test option').length;
//添加一個option
$("#test").append("<option value='9'>ff</option>");
$("<option value='9'>ff</option>").appendTo("#test");
//添除選中項
$('#test option:selected').remove();
//指定項選中
$('#test option:first').remove();
//指定值被刪除
$('#test option').each(function(){
if( $(this).val() == '5'){
$(this).remove();
}
});
$('#test option[value=5]').remove();
//獲取第一個Group的標籤
$('#test optgroup:eq(0)').attr('label');
//獲取第二group下面第一個option的值
$('#test optgroup:eq(1)ption:eq(0)').val();
獲取select中選擇的text與value相關的值
獲取select選擇的Text : var checkText=$("#slc1").find("option:selected").text();
獲取select選擇的value:var checkValue=$("#slc1").val();
獲取select選擇的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex;
獲取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index");
設置select選擇的Text和Value
設置select索引值爲1的項選中:$("#slc1 ").get(0).selectedIndex=1;
設置select的value值爲4的項選中: $("#slc1 ").val(4);
設置select的Text值爲JQuery的選中:
$("#slc1 option[text='jQuery']").attr("selected", true);
PS:特別要注意一下第三項的使用哦。看看JQuery的選擇器功能是如此地強大呀!
添加刪除option項
爲select追加一個Option(下拉項)
$("#slc2").append("<option value='"+i+"'>"+i+"</option>");
爲select插入一個option(第一個位置)
$("#slc2").prepend("<option value='0'>請選擇</option>");
PS: prepend 這是向全部匹配元素內部的開始處插入內容的最佳方式。
刪除select中索引值最大option(最後一個)
$("#slc2 option:last").remove();
刪除select中索引值爲0的option(第一個)
$("#slc2 option[index='0']").remove();
刪除select中value='3'的option
$("#slc2 option[value='3']").remove();
刪除select中text='4'的option
$("#slc2 option[text='3']").remove();