Getting the “newest” selected option's text from multiple select list

后端 未结 3 1439
[愿得一人]
[愿得一人] 2020-12-21 05:25

I have a HTML select list, which can have multiple selects:


                        
    
提交评论

  • 2020-12-21 06:17

    Set a onClick on the option instead of the select:

    $('#mySelect option').click(function() {
        if ($(this).attr('selected')) {
            alert($(this).val());
        }
    });
    
    0 讨论(0)
  • 2020-12-21 06:20
    var val = ''
    $('#mySelect').change(function() {
       newVal = $('#mySelect option:selected').text();
       val += newVal;
       alert(val); # you need this.
       val = newVal;
    });
    

    or let's play some more

    val = ''; 
    $('#id_timezone')
      .focus(
        function(){
            val = $('#id_timezone option:selected').text();
        })
      .change(
        function(){
            alert(val+$('#id_timezone option:selected').text())
        });
    

    Cheers.

    0 讨论(0)
  • 提交回复
    热议问题