How to change a <select> value from JavaScript

后端 未结 10 1520
一生所求
一生所求 2020-11-27 18:36

I have question that seems very simple, but I just can\'t get it right. I have a

提交评论

  • 2020-11-27 18:44
    $('#select').val('defaultValue'); 
    $('#select').change();
    

    This will also trigger events hooked to this select

    0 讨论(0)
  • 2020-11-27 18:44

    I think I know what the problem is here - if you are trying to have it select an option that doesn't exist, it won't work. You need to ADD the option first. Like so:

    var option=document.createElement("option");
    option.text=document.getElementById('other_referee').value
    document.getElementById('referee_new').add(option,null);
    document.getElementById('referee_new').value = document.getElementById('other_referee').value;
    

    Not elegant, but you get the idea.

    0 讨论(0)
  • 2020-11-27 18:46

    Once you have done your processing in the selectFunction() you could do the following

    document.getElementById('select').selectedIndex = 0;
    document.getElementById('select').value = 'Default';
    
    0 讨论(0)
  • 2020-11-27 18:47

    document.getElementById("select").selectedIndex = 0 will work

    0 讨论(0)
  • 2020-11-27 18:48

    If you would like it to go back to first option try this:

       document.getElementById("select").selectedIndex = 0;
    
    0 讨论(0)
  • 提交回复
    热议问题