27
2020
04

JS/Jquery实现select选中option触发事件

js

    选项一
    选项二
复制代码
function gradeChange() {    var myselect = document.getElementById("pid");    var index=myselect.selectedIndex; 
    // selectedIndex代表的是你所选中项的index
    var value = myselect.options[index].value;
    console.log(value);
}
复制代码

jquery

复制代码
<select id="select" >
    <option value="op1">option1</option>
    <option value="op2">option2</option>
    <option value="op3">option3</option>
    <option value="op4">option4</option></select>
复制代码
复制代码
$('#select').change(function() {
    //一:
    console.log($(this).val());
    //二:
    var options=$("#select option:selected");
    console.log(options.val());
})
复制代码

« 上一篇下一篇 »