【问题标题】:changing the selected in list - value issue更改列表中的选定项 - 值问题
【发布时间】:2011-10-16 11:56:36
【问题描述】:

我正在尝试在列表中选择值“111”:

$(this).parent()
       .find("#role1_"+id+" option[value='111']")
       .attr("selected", "selected");

但它不起作用。

当我从它的工作值中删除撇号时(选择的值是'111'):

$(this).parent()
       .find("#role1_"+id+" option[value=111]")
       .attr("selected", "selected");

问题是当值不是数字时(即'abc'):

$(this).parent()
       .find("#role1_"+id+" option[value='abc']")
       .attr("selected", "selected");

$(this).parent()
       .find("#role1_"+id+" option[value=abc]")
       .attr("selected", "selected");

两者都不起作用...

你有什么问题吗?

谢谢!

【问题讨论】:

    标签: jquery option selected


    【解决方案1】:

    试试这个:

    $("#role1_"+id).val("111");
    

    假设 ID 是唯一的,它应该可以正常工作。通过分配下拉元素的值,jQuery 已经负责选择具有该值的选项。

    【讨论】:

      【解决方案2】:

      使用 jQuery 从select 中选择值111 的最简单方法是使用val 方法:

      $('#role1_' + id).val('111');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多