1. 程式人生 > >js怎麼判斷一個物件是文字框(text)還是下拉框(select)

js怎麼判斷一個物件是文字框(text)還是下拉框(select)



方法:一

<input type="text" id="tb_Test" />
< input type="button" id="btn_Test" value="Button />

var type = document.getElementById("tb_Test").type;
if(type == "text"){
 alert("it is input box.");
}
if(type == "button"){
 alert("it is button.");
}

<input type="text" id="txt">
< select id="select"><option>1</option></select>
< script type="text/javascript">
var txt=document.getElementById("txt");
var select=document.getElementById("select");
alert(txt.type);
alert(select.type);

方法:二

<input id="tt"/> <select id="select1" onchange="chengSelect()" <option value="1">test </option <option value="2">test1 </option <option value="3">test2 </option </select <script>   var input = document.getElementById("tt"); var select1 = document.getElementById("select1");
alert(input.tagName); alert(select1.tagName); </script> alert(obj.tagName); if(obj.tagName=="INPUT"){ alert(obj.type); } 獲取值:取select元素中當前選擇的文字內容(而不是選項的值) var tt=$("form select[name=selectName]").find('option:selected').text();
alert(tt);
把selectName換成你表單中的下拉框name值就行了
要是使用ID查詢這樣就行了
var tt=$("#selectId").find('option:selected').text();