Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

jquery - 设置选择框的选定选项(Set selected option of select box)

I want to set a option that was selected previously to be displayed on page load.

(我想设置一个先前选择的选项,以便在页面加载时显示。)

I tried it with the following code:

(我用以下代码尝试了它:)

$("#gate").val('Gateway 2');

with

(同)

<select id="gate">
    <option value='null'>- choose -</option>
    <option value='gateway_1'>Gateway 1</option>
    <option value='gateway_2'>Gateway 2</option>
</select>

But this does not work.

(但这不起作用。)

Any ideas?

(有任何想法吗?)

  ask by Upvote translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This definitely should work.

(这肯定应该工作。)

Here's a demo .

(这是一个演示 。)

Make sure you have placed your code into a $(document).ready :

(确保已将代码放入$(document).ready :)

$(function() {
    $("#gate").val('gateway_2');
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...