【问题标题】:jquery remove selected attribute on option not working IE 7 & 8jquery 删除选项不工作的选定属性 IE 7 和 8
【发布时间】:2013-07-12 20:11:42
【问题描述】:

我一直在努力解决这个问题。我所拥有的是彼此相邻的父/子选择框,它们是根据选择的父对象动态生成的。

我想做的是,如果我选择上一级,我会删除选定子选项上的选定属性。

下面的代码解释了我想做什么。

    <div id="selectBoxes">
      <select>
        <option value=""></option>
        <option value="parent">Parent</option>
      </select>
      <select class="mySelect">
        <option value=""></option>
        <option value="child">Child</option>
      </select>
      <select class="mySelect">
        <option value=""></option>
        <option value="grandChild">Grandchild</option>
      </select>
    </div>
    <script type="text/javascript">
      $('select').change(function() {
          var $nextSelects = $(this).nextAll('.mySelect');
          $nextSelects.each(function() {
                $(this).find('option:selected').prop("selected" , false);
           });
      });
    </script>

可以找到一个例子here

所以需要发生的是,如果选择了所有 3 个选项并且我返回让我们说第一个选择 2 个子选择被取消选择。上述代码适用于 FF 和 Chrome 以及 IE 9,但不适用于 IE 7 & 8。

我已尝试使用 .attr("selected" , "") & .removeAttr("selected") 在 IE 中仍然无法使用。

非常感谢任何帮助。

【问题讨论】:

  • 你检查我的答案了吗

标签: jquery internet-explorer-8 internet-explorer-7 selected


【解决方案1】:

试试这样:

$('select').change(function () {
    var $nextSelects = $(this).nextAll('.mySelect');
    $nextSelects.each(function () {
        $(this).val('');
    }); 
});

【讨论】:

    【解决方案2】:

    这在 IE8 中工作我没有 IE7 请检查并恢复

    $('select').change(function () {
        var $nextSelects = $(this).nextAll('.mySelect');
        $nextSelects.each(function () {
          $(this).val("").prop("selected", true); 
        });
    });
    

    DEMO

    第二种方法

    $('select').not(".mySelect").change(function () {      
        $(this).siblings('.mySelect').each(function () {
            $(this).find('option[value=""]').prop('selected', true);        
        });
    });
    

    DEMO

    以下也可以使用

    $(this).val("").prop("selected", true);

    DEMO

    注意 jQuery1.10 在 IE6,7,8 中不支持见here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2011-11-30
      • 2012-05-14
      • 2011-01-30
      • 1970-01-01
      • 2013-12-13
      相关资源
      最近更新 更多