1. 程式人生 > 其它 >原生js、jquery下拉框選中內容的獲取

原生js、jquery下拉框選中內容的獲取

技術標籤:前端

<select id="sel">
    <option value="add">增加</option>
    <option value="low">減少</option>
</select>

下拉框可以選中某個選項後,可以獲取兩個內容,一個是選中後,顯示的文字的值,一個是選中後,顯示文字對應的value的值

獲取選中後,顯示的文字的值

//原生js
var sel = document.getElementById('sel')
var index = sel.selectedIndex
var text = sel.options[index].text

//jquery
$('#sel option:selected').text()

獲取選中後,顯示文字對應的value值

//原生js
var sel = document.getElementById('sel')
var index = sel.selectedIndex
var value = sel.options[index].value

//jquery
$('#sel').val()