【问题标题】:removeAttr doesn't remove disabled attribute in Firefox [duplicate]removeAttr 不会删除 Firefox 中的禁用属性 [重复]
【发布时间】:2013-09-26 13:46:32
【问题描述】:

我有以下代码,通过使用 removeAttr 函数删除 disabled 属性来解锁下拉列表。此示例在 Mozilla Firefox 24 for Ubuntu 中不起作用。但是,如果在 RemoveAttr 函数之后添加一个警报,它就可以正常工作,如下一个示例所示:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

工作示例:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            alert("REMOVED");
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

.prop 的示例也不起作用:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {
            $('#dropdown2').prop('disabled', false);
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);

        }
        else
        {
            $('#dropdown2').prop('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

【问题讨论】:

  • 有很多关于 .attr() 方法的文档...To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. 仅供参考,不要使用 removeProp() 代替 removeAttr(),而是将属性设置为 false
  • 能否分享相关的html
  • obj的值
  • 如果问题仍然存在,您应该创建一个jsfiddle.net 重现此问题
  • 好吧,看起来 Select2 插件搞乱了这些下拉菜单。删除后,一切正常。

标签: jquery firefox


【解决方案1】:

您应该使用.prop() 设置禁用的属性状态

启用

$('#dropdown2').prop('disabled', false);

禁用

$('#dropdown2').prop('disabled', true);

阅读:Attributes vs. Properties

【讨论】:

  • 同样的事情,仍然不起作用: $("#dropdown1").change(function() { $('#dropdown2').find('option').remove(). end(); if (obj[$(this).val()] !== undefined) { $('#dropdown2').prop('disabled', false); $('#dropdown2').append( '' + obj[$(this).val()]); $('#dropdown2').attr('required', true); } else { $('#dropdown2') .prop('disabled', true); $('#dropdown2').attr('required', false); } });
【解决方案2】:

来自(文档)(http://api.jquery.com/attr/#entry-longdesc):

从 jQuery 1.6 开始,.attr() 方法返回未定义的属性 尚未设置。检索和更改 DOM 属性,例如 表单元素的选中、选择或禁用状态,使用 .prop() 方法。

所以你应该改用.prop() 函数。

$('#dropdown2').prop('disabled', false); // Enables the element
$('#dropdown2').prop('disabled', true ); // Disables the element

【讨论】:

  • 同样的问题。不工作
  • @user1029829 如果问题仍然存在,您应该创建一个 jsfiddle.net 来重现此问题
【解决方案3】:

使用prop

$('#dropdown2').prop('disabled', false);

disable true 禁用元素,禁用 false 启用元素

【讨论】:

  • 同样的问题。没用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 2011-11-30
  • 1970-01-01
  • 2017-04-15
  • 2015-07-26
  • 2014-11-08
  • 2014-06-20
相关资源
最近更新 更多