Jquery select change

后端 未结 5 1435
攒了一身酷
攒了一身酷 2021-02-19 08:12

                        
    
提交评论

  • 2021-02-19 08:35

    The 2nd alert will return null, because <select> has no attribute prefix.

    0 讨论(0)
  • 2021-02-19 08:41

    What is it you are expecting exactly?

    I find that the second alert - alert( $(this).attr('prefix') ); is the one causing a problem. As is, you get an alert of the prefix number, then an alert of null (caused by the second alert).

    0 讨论(0)
  • 2021-02-19 08:55

    In your code, $(this) refers to the <select>, not the option. The prefix attribute does not exist on the <select>

    This will cause the problem with the second example.

    0 讨论(0)
  • 2021-02-19 08:56

    You probably wanted .val() not .attr('prefix'), to obtain selected value of <select>.

    $(document).ready(function() {  
        $('#Nazione').change(function(){
    
            alert( $(this).find("option:selected").attr('prefix') );
            alert( $(this).val('prefix') );
        });
      });
    
    0 讨论(0)
  • 提交回复
    热议问题