Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何防止输入被禁用?_Javascript_Html_Disabled Input - Fatal编程技术网

Javascript 如何防止输入被禁用?

Javascript 如何防止输入被禁用?,javascript,html,disabled-input,Javascript,Html,Disabled Input,我有一个id为“我的字段”的输入字段,一些插件在第一次转义后向该字段添加属性disabled=“disabled” 这是一个问题,因此我尝试使用以下代码使禁用的属性远离它: var input = document.getElementById("my-field"); document.addEventListener("change", input, function(){ setTimeout(function() { input.disabled = false;

我有一个id为“我的字段”的输入字段
,一些插件在第一次转义后向该字段添加属性
disabled=“disabled”

这是一个问题,因此我尝试使用以下代码使禁用的属性远离它:

var input = document.getElementById("my-field");
document.addEventListener("change", input, function(){
    setTimeout(function() {
        input.disabled = false;
    }, 50)
});
但它不起作用,我做错了什么


编辑:如下所示,我将布尔值更正为没有“”,但它不起作用。

您可以这样设置它

香草Javascript

JQuery


你可以这样设置

香草Javascript

JQuery


如果您只想删除属性,请像下面这样使用
removeAttribute
函数:

document.querySelector('#my-field').removeAttribute('disabled')
new MutationObserver(([{ attributeName, target }]) =>
        attributeName === 'disabled' && target.removeAttribute('disabled')
    ).observe(document.querySelector('#my-field'), { attributes: true });
至于您的解决方案,我建议您倾听元素的更改,而不是猜测超时,类似这样:

document.querySelector('#my-field').removeAttribute('disabled')
new MutationObserver(([{ attributeName, target }]) =>
        attributeName === 'disabled' && target.removeAttribute('disabled')
    ).observe(document.querySelector('#my-field'), { attributes: true });

如果您只想删除属性,请像下面这样使用
removeAttribute
函数:

document.querySelector('#my-field').removeAttribute('disabled')
new MutationObserver(([{ attributeName, target }]) =>
        attributeName === 'disabled' && target.removeAttribute('disabled')
    ).observe(document.querySelector('#my-field'), { attributes: true });
至于您的解决方案,我建议您倾听元素的更改,而不是猜测超时,类似这样:

document.querySelector('#my-field').removeAttribute('disabled')
new MutationObserver(([{ attributeName, target }]) =>
        attributeName === 'disabled' && target.removeAttribute('disabled')
    ).observe(document.querySelector('#my-field'), { attributes: true });
$(“#我的字段”).removeAttr(“禁用”); document.getElementById(“我的字段”).removeAttribute(“禁用”)

$(“#我的字段”).removeAttr(“禁用”);
document.getElementById(“我的字段”).removeAttribute(“已禁用”)

使用jquery解决您的问题

对于启用: $(“#元素”).prop(“禁用”,false)

对于禁用:
$(“#元素”).prop(“禁用”,true)

使用jquery解决您的问题

对于启用: $(“#元素”).prop(“禁用”,false)

对于禁用:
$(“#元素”).prop(“禁用”,true)

尝试在输入中重新定义setAttribute方法和disabled属性,例如:

function removeDisabled(input) {
    input.disabled = false;
    input.removeAttribute('disabled');
    Object.defineProperties(input, {
        disabled: {
            get: function() { return false; }
        },
        setAttribute: {
            value: function() {}
        }
    })
}

removeDisabled(document.getElementById('my-field'))

尝试在输入中重新定义setAttribute方法和disabled属性,例如:

function removeDisabled(input) {
    input.disabled = false;
    input.removeAttribute('disabled');
    Object.defineProperties(input, {
        disabled: {
            get: function() { return false; }
        },
        setAttribute: {
            value: function() {}
        }
    })
}

removeDisabled(document.getElementById('my-field'))


字符串值“false”与布尔值
false
不同。请更正它,谢谢,但它不起作用,它还抛出了关于eventListener第二个参数的错误,因此我将输入变量放在那里,仍然没有luckThe
addEventListener()
API不起作用。可以将侦听器直接添加到元素中。上面介绍的是如何使用jQuery,在某种程度上,您将jQuery作为标记包含在问题中。如果你不是真的使用jQuery,你应该删除标记。没错,我很久没有在js中编码了,我混淆了jQuery版本,你是对的,我不能使用jqeury我删除了标记字符串值“false”与布尔值
false
不同。更正了谢谢,但它不起作用,它还抛出了关于eventListener第二个参数的错误,因此我将输入变量放在那里,仍然没有luckThe
addEventListener()
API不能以这种方式工作。可以将侦听器直接添加到元素中。上面介绍的是如何使用jQuery,在某种程度上,您将jQuery作为标记包含在问题中。如果你不是真的使用jQuery,你应该删除标记。没错,我很久没有用js编码了,我混淆了jQuery版本,你是对的,我不能使用jqeury,我删除了标记。道具对香草也有用吗?因为我现在没有jquery为你添加普通的解决方案不起作用,它与我的解决方案相同,但布尔值上没有“”,正如上面@Pointy所建议的,如果你的问题是覆盖了其他插件禁用输入,然后,您应该在事件侦听器外部编写
setTimeout
,因为如果输入被defaultdoes禁用,则永远不会触发输入更改事件。prop是否也适用于vanilla?因为我现在没有jquery为你添加普通的解决方案不起作用,它与我的解决方案相同,但布尔值上没有“”,正如上面@Pointy所建议的,如果你的问题是覆盖了其他插件禁用输入,然后,您应该在事件侦听器外部写入
setTimeout
,因为如果输入被default禁用,则永远不会触发输入更改事件尝试使用getElementById而不是querySelector,否luck@NathanBernard函数“getElementById”不是泛型函数,它的名称更长、更麻烦,并且仅指ID。如果要使用包含类或属性的选择器,则需要替换整个函数。在“querySelector”的情况下,控件更方便,并且只应替换选择器。您的解决方案是否应该与Observer一起工作?因为我尝试了,但没有成功(我显然在queryselector中设置了正确的#id)。thanks@NathanBernard看,您正在将请求的元素作为参数传递给它。如果这个元素还不在DOM中,那么当然什么也不会发生。如果此
document.querySelector(“#我的字段”)
返回
null
,它将不起作用。这就是我使用.addEventListener(“更改”,输入,[…]以文档为目标,但如上所述,我混淆了jquery和vanilla,因此我需要在vanilla中使用它的等价物,我猜是用getElementById而不是querySelector,不是吗luck@NathanBernard函数“getElementById”不是泛型的,只引用ID。如果要使用包含类或属性的选择器,则需要替换整个函数。对于“querySelector”,控件更方便,只需更换选择器。使用Observer的解决方案是否应该立即生效?因为我尝试过,但没有生效(我显然在queryselector中设置了正确的#id). thanks@NathanBernard看,您正在将请求的元素作为参数传递给它。如果这个元素还不在DOM中,那么当然什么也不会发生。如果这个
文档.querySelector(“#我的字段”)
返回
null
,它将不起作用。这就是为什么我使用.addEventListener(“更改”,输入,[…]以文档为目标,但如上所述,我混淆了jquery和vanilla,因此我需要在