【问题标题】:How do I get the value of a select menu with jquery on change如何在更改时使用 jquery 获取选择菜单的值
【发布时间】:2011-12-13 05:55:44
【问题描述】:

我有一个选择菜单,我正在尝试绑定一个点击功能,并且能够在更改时恢复选择选项值属性。

             select = $('#select_networks').selectmenu();

            //bind the change of network/group
            select.bind( "change", function(event, ui) {

              //need to figure out the selected elements value attribute

            });

【问题讨论】:

    标签: jquery jquery-ui jquery-mobile


    【解决方案1】:

    看这个:

    $("#select_networks").bind("change", function() {
        alert($(this).val());
    });
    

    更新

    $("#select_networks").bind("change", function() {
        var typeVal = $(this).children("*[value=" + $(this).val() + "]").attr("type");
        alert(typeVal);
    });
    

    【讨论】:

    • 这个值很好,但后续问题。我如何也可以提取任意属性。例如我在选择选项中设置了一个类型字段,我尝试了 alert($(this).attr("type"));但我只是不确定。
    【解决方案2】:
    this.value
    

    通常你可以将它与表单元素一起使用,它比将 this 包装为 jQuery 对象然后提取值要快得多。

    然而,在某些情况下,特别是选择的,您可以在旧版本中遇到IE旧版本的问题,当没有excixity value=""和jQuery为您提供...
    $(this).val();
    

    【讨论】:

      【解决方案3】:
              select.bind( "change", function(event, ui) {
      
                  $(this).children(':selected').val();
      
              });    
      

      这将选择选定的<option>(s)。

      这是一个演示:http://jsfiddle.net/4KS9z/1/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        相关资源
        最近更新 更多