Answers:
由于此设置不是属性
这是一个属性。
一些属性定义为布尔值,这意味着您可以指定它们的值,而忽略其他所有内容。即,您只包括粗体部分,而不是disabled =“ disabled ”。在HTML 4中,您应该仅包括粗体部分,因为完整版被标记为功能受支持的功能(尽管与编写规范时相比,现在情况不那么正确了)。
从HTML 5开始,规则已更改,现在您仅包括名称,而不包括值。因为名称和值相同,所以这没有实际区别。
该DOM属性也被称为disabled
并且是采用布尔true
或false
。
foo.disabled = true;
从理论上讲,您也可以foo.setAttribute('disabled', 'disabled');
和foo.removeAttribute("disabled")
,但是我不相信Internet Explorer的较早版本(就臭名昭著的Bug而言setAttribute
)。
foo
中foo.disabled = true;
?是那个按钮的ID吗?
禁用
document.getElementById("btnPlaceOrder").disabled = true;
启用
document.getElementById("btnPlaceOrder").disabled = false;
$('#btnPlaceOrder').disabled = false;
没有帮助。
$('#btnPlaceOrder')[0].disabled = false
jquery选择器似乎返回一个数组。耸耸肩。
它是一个属性,但是是一个布尔值(因此它不需要名称,只需要一个值-我知道,这很奇怪)。您可以在Javascript中设置等效的属性:
document.getElementsByName("myButton")[0].disabled = true;
尝试以下方法:
document.getElementById("id").setAttribute("disabled", "disabled");
在上设置disabled
属性的官方方法HTMLInputElement
是:
var input = document.querySelector('[name="myButton"]');
// Without querySelector API
// var input = document.getElementsByName('myButton').item(0);
// disable
input.setAttribute('disabled', true);
// enable
input.removeAttribute('disabled');
尽管@kaushar的答案足以启用和禁用an HTMLInputElement
,并且由于IE的历史悠久的错误原因,可能对于跨浏览器兼容性更可取setAttribute
,但它仅适用于Element
属性影子Element
属性。如果设置了属性,则DOM默认使用属性的值,而不是等效属性的值。
属性和属性之间存在非常重要的区别。true HTMLInputElement
属性的示例是input.value
,下面演示了阴影如何工作:
var input = document.querySelector('#test');
// the attribute works as expected
console.log('old attribute:', input.getAttribute('value'));
// the property is equal to the attribute when the property is not explicitly set
console.log('old property:', input.value);
// change the input's value property
input.value = "My New Value";
// the attribute remains there because it still exists in the DOM markup
console.log('new attribute:', input.getAttribute('value'));
// but the property is equal to the set value due to the shadowing effect
console.log('new property:', input.value);
<input id="test" type="text" value="Hello World" />
那就是说属性遮盖属性的意思。这个概念也适用于prototype
链上的继承属性:
我希望这可以澄清有关属性和属性之间差异的任何混淆。
它仍然是一个属性。设置为:
<input type="button" name=myButton value="disable" disabled="disabled">
... 已验证。
我认为最好的方法可能是:
$("#ctl00_ContentPlaceHolder1_btnPlaceOrder").attr('disabled', true);
它可以很好地跨浏览器。
prop
,不是attr
。
<button disabled=true>text here</button>
您仍然可以使用属性。只需使用“禁用”属性代替“值”即可。
disabled="disabled"
或只是disabled
。任何字符串值都等效于"disabled"
,包括disabled="true"
和disabled="false"
。