开发者

How to select a drop-down option based on a value?

开发者 https://www.devze.com 2023-04-05 11:57 出处:网络
I have the following drop-down: <select id=\"#test\"> <option value=\"1\">1</option>

I have the following drop-down:

<select id="#test">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Now I am getting a value on document.ready like:

var res = $('#someotherid').val();

which is 1,2 or 3.

Now I want this value automatically set in the drop-down.

Does anyone know how t开发者_Python百科o do this?


You can set the selected option with the val method too, so after you get the value of whatever #someotherid is, you can use it to set the selected option of #test:

var res= $('#someotherid').val();
$("#test").val(res);

Here's a working example.

0

精彩评论

暂无评论...
验证码 换一张
取 消