天天看点

JQuery中对Select的option项的添加、删除、取值(带输入框)

html实现带输入框的select

<span class="span">
	<select id="door_mode" class="editable" οnchange="change('door_mode')">
		<option value="-1">门类型</option>
	</select>
	<input type="text" id="door_txt" class="input" οnfοcus="focusclean(this)" οnchange="getDataByname(4,'door_txt')"/>
</span>
.span{ width:84px; height:28px; border:1px #ccc solid; position:relative; margin-top : 10px; }
.span .editable{ width:100%; height:100%; position:absolute; left:0px; top:0px; z-index:0 }
.span .input{ width:78%; height:100%; background:#eee; position:absolute; left:0px; top:0px; z-index:1000; font-size: 10px; }
           

JS获取Select选择的Text和Value: 

funtion change(id){
	var obj = document.getElementById(id);
	var index = obj.selectedIndex;//获取选中项的索引index
	var value = obj.options[index].value;//获取选中项的value
	var text = obj.options[index].text;//获取选中项的text
	$(obj).siblings("input").val(text);//在输入框中设text值
}
           

jQuery获取Select选择的Text和Value: 

function change(id){
	var checkText=$(id).find("option:selected").text(); //获取Select选择的Text 
	var checkValue=$(id).val(); //获取Select选择的Value 
	var checkIndex=$(id).get(0).selectedIndex; //获取Select选择的索引值 
	var maxIndex=$(id+" option:last").attr("index"); //获取Select最大的索引值 
}
           

jQuery添加/删除Select的Option项

$("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项) 
$("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置) 
$("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个) 
$("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个) 
$("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option 
$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option 
$("#select_id ").empty(); //清空所有的option
$("#select_id ").siblings('input').val($("#select_id ").children("option").first().text());//在输入框中设当前option中的第一项为其值