【问题标题】:How to make option selected in jquery by Name [duplicate]如何按名称在jquery中选择选项[重复]
【发布时间】:2015-07-14 22:41:31
【问题描述】:

如何在 jquery 中按名称选择选项

这里是fiddle

这是脚本

<select name="zoom_cat[]" style="margin-left:15px;" id="contact_country">
    <option value=""  selected="selected">Nothing</option>

  <option value="0">Singam</option>
  <option value="1">Karadi</option>
  <option value="2">Poonai</option>
  <option value="3">Yaanai</option>
</select>

这是脚本

var myText = "Singam";
        $("#contact_country option:text=" + myText +"").prop("selected", "selected");

【问题讨论】:

    标签: javascript php jquery


    【解决方案1】:

    使用:contains 选择器

    $("#contact_country option:contains('" + myText +"')").prop("selected", "selected");
    

    演示:Fiddle


    注意:使用 contains 可以返回部分结果,因此迭代并查找元素 ex:http://jsfiddle.net/arunpjohny/54w5rq5m/2/ 可能更安全

    所以

    var myText = "Singam";
    $("#contact_country option").each(function () {
        if ($(this).text() == myText) {
            $(this).prop("selected", true);
            //since element is found no need to iterate further
            return false;
        }
    });
    

    演示:Fiddle

    【讨论】:

    • 脚本应该在选项选择之前或之后加载??
    • 如果它在 dom 就绪处理程序中没关系.. else after
    • 感谢您的回答 :)
    【解决方案2】:

    您还可以在选项上使用filter 以实现完全匹配。包含也为您提供部分匹配。

    $('#contact_country option').filter(function() {
                        return $.trim($(this).text()) === myText;
                    }).prop('selected', true);
    

    【讨论】:

      【解决方案3】:

      使用:contains() 代替:text。试试 -

          var myText = "Singam";
          $("#contact_country option:contains(" + myText +")").attr("selected", true);
      

      【讨论】:

        【解决方案4】:

        你可以使用类似的东西:

        HTML:

        <a href="#" id="test" class="mytest">One</a>
        

        两个 三

        Javascript:

        (function ($, window, document, undefined) {
        $.scrollTo = function(el, options){
            // To avoid scope issues, use 'base' instead of 'this'
            // to reference this class from internal events and functions.
            var base = this;
        
            // Access to jQuery and DOM versions of element
            base.$el = $(el);
            base.el = el;
        
            base.init = function(){
                base.options = $.extend({},$.scrollTo.defaultOptions, options);
        
                // Put your initialization code here
            };
        
            // On click
          base.$el.click(function (event) {
                event.preventDefault(); 
                alert("clicked!");
            });    
        
            // Run initializer
            base.init();
        };
        
        $.scrollTo.defaultOptions = {};
        
        $.fn.scrollTo = function(options){
            return this.each(function(){
                (new $.scrollTo(this, options));
            });
        };
        

        })(jQuery, 窗口, 文档);

        //初始化插件 $('.mytest').scrollTo();

        谢谢

        【讨论】:

          猜你喜欢
          • 2016-11-28
          • 2021-07-19
          • 2012-04-28
          • 2015-06-27
          • 2015-12-11
          • 2017-04-08
          • 1970-01-01
          • 2013-09-12
          • 1970-01-01
          相关资源
          最近更新 更多