JQuery - 如何根据值选择下拉项
我想设置一个下拉列表(选择)以根据条目的值进行更改。
我有
<select id="mySelect">
<option value="ps">Please Select</option>
<option value="ab">Fred</option>
<option value="fg">George</option>
<option value="ac">Dave</option>
</select>
而且我知道我想更改下拉列表,以便选择值为“fg”的选项。我怎样才能用 JQuery 做到这一点?
I want set a dropdown(select) to be change based on the value of the entries.
I have
<select id="mySelect">
<option value="ps">Please Select</option>
<option value="ab">Fred</option>
<option value="fg">George</option>
<option value="ac">Dave</option>
</select>
And I know that I want to change the dropdown so that the option with the value of "fg" is selected. How can I do this with JQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您应该使用
以下示例:
You should use
Here's an example:
(可选)
其中
3
是所需选项的索引。现场演示
Optionally,
where
3
is the index of the option you want.Live Demo
你可以使用这个 jQuery 代码,我发现它更容易使用:
You can use this jQuery code which I find it eaiser to use:
您可以简单地使用:
You can simply use:
在你的情况下
$("#mySelect").val("fg")
:)In your case
$("#mySelect").val("fg")
:)回答可能为时已晚,但至少有人会得到帮助。
您可以尝试两种选择:
这是当您想要基于索引值进行分配时的结果,其中“0”是索引。
不要使用“attr”,因为最新的 jquery 已弃用它。
当您想根据选项值进行选择时,请选择以下选项:
其中“fg”是选项值
May be too late to answer, but at least some one will get help.
You can try two options:
This is the result when you want to assign based on index value, where '0' is Index.
don't use 'attr' since it is deprecated with latest jquery.
When you want to select based on option value then choose this :
where 'fg' is the option value
这段代码对我有用:
使用这个 HTML 选择列表:
This code worked for me:
With this HTML select list:
我有一种不同的情况,下拉列表值已经被硬编码。只有 12 个区,因此 jQuery Autocomplete UI 控件不由代码填充。
解决方案要容易得多。因为我不得不费力地浏览其他帖子,其中假设控件是动态加载的,没有找到我需要的东西,然后最终弄清楚了。
因此,如果您有如下所示的 HTML,请像这样设置所选索引,请注意 -input 部分,它是除了下拉 id 之外的部分:
关于自动完成控件的其他问题是如何正确禁用/清空它。我们有 3 个控件一起工作,其中 2 个是相互排斥的:
其中一些超出了帖子的范围,我不知道到底该把它放在哪里。由于这非常有帮助并且花了一些时间才弄清楚,因此将其共享。
另外...要启用这样的控件,它是(禁用,假)而不是(启用,真)——这也花了一些时间来弄清楚。 :)
除了这篇文章之外,唯一需要注意的是:
所有内容都是共享的,因为即时学习这些东西花了太长时间。希望这有帮助。
I have a different situation, where the drop down list values are already hard coded. There are only 12 districts so the jQuery Autocomplete UI control isn't populated by code.
The solution is much easier. Because I had to wade through other posts where it was assumed the control was being dynamically loaded, wasn't finding what I needed and then finally figured it out.
So where you have HTML as below, setting the selected index is set like this, note the -input part, which is in addition to the drop down id:
Something else figured out about the Autocomplete control is how to properly disable/empty it. We have 3 controls working together, 2 of them mutually exclusive:
Some of this is beyond the scope of the post and I don't know where to put it exactly. Since this is very helpful and took some time to figure out, it's being shared.
Und Also ... to enable a control like this, it's (disabled, false) and NOT (enabled, true) -- that also took a bit of time to figure out. :)
The only other thing to note, much in addition to the post, is:
All shared because it took too long to learn these things on the fly. Hope this is helpful.
您可以按名称选择下拉选项值
You can select dropdown option value by name
两者均可用于获取所选选项值
either can be used to get the selected option value