How do I programmatically set the value of a select box element using JavaScript?

后端 未结 17 1891
轮回少年
轮回少年 2020-11-22 06:33

I have the following HTML

17条回答
  •  执笔经年
    2020-11-22 06:55

    The easiest way if you need to:
    1) Click a button which defines select option
    2) Go to another page, where select option is
    3) Have that option value selected on another page

    1) your button links (say, on home page)

    Sales
    IT
    

    (where contact.php is your page with select options. Note the page url has ?option=1 or 2)

    2) put this code on your second page (my case contact.php)

    
    

    3) make the option value selected, depending on the button clicked

    
    

    .. and so on.
    So this is an easy way of passing the value to another page (with select option list) through GET in url. No forms, no IDs.. just 3 steps and it works perfect.

提交回复
热议问题