Jquery to get the id of selected value from dropdown

前端 未结 4 1220
面向向阳花
面向向阳花 2021-02-10 14:26

I have a drop down list that fetches values from db as follows

$.get(\'/getJobs\', function (jobs) {
        seljobs = jobs;
        var i = 0;
        jobs.forE         


        
4条回答
  •  北海茫月
    2021-02-10 15:08

    Try this

    on change event

    $("#jodSel").on('change',function(){
        var getValue=$(this).val();
        alert(getValue);
      });
    

    Note: In dropdownlist if you want to set id,text relation from your database then, set id as value in option tag, not by adding extra id attribute inside option its not standard paractise though i did both in my answer but i prefer example 1

    HTML Markup

    Example 1:
        
    Example 2 :
        
    

    Jquery:

    $("#example1").on('change', function () {
        alert($(this).val());
    });
    
    $("#example2").on('change', function () {
        alert($(this).find('option:selected').attr('id'));
    });
    

    View Demo : For example 1 & 2

    Blog Article : Get and Set dropdown list selected value with Jquery

    My Blog : jQuery tutorials

提交回复
热议问题