根据节点值在下拉菜单中选择一个选项? [英] Selecting an option in a dropdown menu based on node value?

查看:84
本文介绍了根据节点值在下拉菜单中选择一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据节点值在jQuery(或Javascript)中设置下拉列表的值?

Is there a way to set the value of a dropdown list in jQuery (or Javascript) based on the node value?

 <select name="ddlProperty">
        <option value="1" selected="selected"></option>
        <option value="2">Animal Kingdom</option>
        <option value="3">Epcot</option>
        <option value="4">Hollywood Studios</option>
        <option value="5">Magic Kingdom</option>
        <option value="6">Downtown Disney</option>
</select>

我需要设置Magic Kingdom的选项,如下所示:

I'd need to set the option of Magic Kingdom, so something like:

$("#ddlLocation").val("Magic Kingdom")

因此,Magic Kingdom将成为所选项目,但不会按预期工作。有什么想法吗?

So that Magic Kingdom would become the selected item, that doesn't work as expected. Any ideas?

推荐答案

如果你可以使用(不是文本!),那就用 .val()

If you can use the value (not text!), do that using .val():

$("#ddlProperty").val("5");

如果你没有,请使用 .filter() .text() .attr() 查找并设置所选的< option> ,像这样:

If you don't have that, use .filter(), .text() and .attr() to find and set the selected <option>, like this:

$("#ddlProperty option").filter(function() {
  return $(this).text() === "Magic Kingdom"
}).attr('selected', true);

这篇关于根据节点值在下拉菜单中选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆