1. 程式人生 > >下拉框獲取選中值

下拉框獲取選中值

一:javascript原生的方法

  1:拿到select物件: var  myselect=document.getElementById("select的ID");

  2:拿到選中項的索引:var index=myselect.selectedIndex ;             // selectedIndex代表的是你所選中項的index

  3:拿到選中項options的value:  myselect.options[index].value;

  4:拿到選中項options的text:  myselect.options[index].text;

二:jquery方法(前提是已經載入了jquery庫)

1:var options=$("#test option:selected");  //獲取選中的項

2:alert(options.val());   //拿到選中項的值

3:alert(options.text());   //拿到選中項的文字