启用所有禁用的输入并选择元素

时间:2013-06-25 05:28:33

标签: jquery html performance

我希望在提交表单之前启用所有已禁用的输入并选择元素,这样我就可以获取所有已禁用的元素值。

所以我试过

function enable() {

        $('input[type="text"], input[type="checkbox"], select').prop("disabled", true).each(function () {

            if ($(this).is(':disabled')) {
                $(this).attr('disabled', false);
            }

        });
    }

此代码无效,请您帮我完成这项工作。

提前致谢....

4 个答案:

答案 0 :(得分:8)

你可以尝试

function enable() {
    $('input:disabled, select:disabled').each(function () {
       $(this).removeAttr('disabled');
    });
}

http://api.jquery.com/disabled-selector/

答案 1 :(得分:3)

试试这个,

function enable() {
    $('input[type="text"], input[type="checkbox"], select').each(function () {
       if ($(this).prop('disabled')) {
           $(this).prop('disabled',false);
       }
    });
}

或者,您可以在不使用each()的情况下启用所有已禁用的元素,

$('input[type="text"]:disabled, input[type="checkbox"]:disabled, select:disabled')
          .prop('disabled',false);

或者使用removeAttr()之类的,

$('input[type="text"]:disabled, input[type="checkbox"]:disabled, select:disabled')
          .removeAttr('disabled');

答案 2 :(得分:2)

$(this).removeAttr('disabled');

答案 3 :(得分:0)

使用prop not attr,.prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性。

 $(this).prop('disabled',false);
相关问题