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

时间:2024-03-05 11:57:08

js

<select id="pid" onchange="gradeChange()">
    <option value="a">选项一</option>
    <option value="b">选项二</option>
</select>
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());
})