How can I add an option in the beginning?

前端 未结 3 1813
南笙
南笙 2021-01-12 09:01

I have html select,


                        
    
提交评论

  • 2021-01-12 09:31

    First, you mean append, rather than appendTo. append adds the content of the parameter to the selection; appendTo appends the selection to the element designated in the parameter.

    Second, perhaps intuitively, you can use prepend:

    $('#myselect').prepend($('<option>my-option</option>')); 
    
    0 讨论(0)
  • 2021-01-12 09:32

    Use prependTo()

    $('#myselect').prependTo($('my-option'));//typo? 
    
    You might have meant this

    $('#myselect').prepend($('<option>my-option</option>'));  
    

    http://api.jquery.com/prependTo

    http://api.jquery.com/prepend/

    But your code is actually the other way around. This might be a typo. In any case, the methods available are prepend() and prependTo()

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