简体   繁体   中英

jquery Setting option selected with known value

I have the next select:

<select name="priority" id="priority" class="update_priority">
    <option value="root" label="Without parent">Without parent</option>
    <option value="72" label="Rank3">Rank3</option>
    <option value="71" label="Rank1">Rank1</option>
    <option value="67" label="Rank2">Rank2</option>
    <option value="64" label="Rank4">Rank4</option>
</select>

In JS i have a variable with something value. For example:

selected = 71;

Now, with help of jQuery I want to make option selected with this value(in our example 71).

you don't need jquery for this. you can just use plain old javascript:

document.getElementById("priority").value = "71";

But if you still want to with jquery you can do the same thing:

$("#priority").val("71")

EDIT: Here is an example. You can comment out either one you want to see the other one work: http://jsfiddle.net/BupWA/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM