JS/Jquery實現select選中option觸發事件


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());
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM