javascript 从 html 下拉列表和文本框中删除禁用的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14613736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove disabled attribute from html dropdown and textbox
提问by Jt2ouan
I am running a c# webpage with html buttoms and am having trouble executing the javascript that enables the dropdown and textboxes. They are all predefined as disabled as well as a ssave button as hidden and on button click I call NewType() function and they should be visible and enabled on button click but its not working. The call for visibility none on the save button works but not for the drop down or textboxes.
我正在运行带有 html 按钮的 ac# 网页,并且在执行启用下拉列表和文本框的 javascript 时遇到问题。它们都被预定义为禁用以及一个隐藏的 ssave 按钮,在按钮单击时我调用 NewType() 函数,它们应该在按钮单击时可见并启用,但它不起作用。保存按钮上的可见性调用无效,但不适用于下拉列表或文本框。
<script type="text/javascript">
$(document).ready(function() {
var d = document.getElementById('saveBtn').style.visibility = 'hidden';
});
function NewType() {
var e = document.getElementById('saveBtn').style.visibility = 'visible';
var x = document.getElementById('DD').removeAttribute('disabled');
var a = document.getElementById('txt1').removeAttribute('disabled');
var b = document.getElementById('txt2').removeAttribute('disabled');
var c = document.getElementById('txt3').removeAttribute('disabled');
};
<button type="button" onclick="NewType()">Add New</button>
<button type="button" id="saveBtn">Save</button>
<select ID="DD" runat="server" disabled="disabled"
height="30px" style="width: 120px">
<option value=""></option>
<option value="1">fruit</option>
<option value="2">vegetables</option>
<option value="3">Other</option></select>
<br />
<input size="3" type="text" ID="txt1" runat="server" disabled="disabled" />
<input size="3" type="text" ID="txt2" runat="server" disabled="disabled"/>
<input size="4" type="text" ID="txt3" runat="server" disabled="disabled" />
回答by Fenton
If you don't need it, removing the runat="server"
attribute from those elements. Controls that have this attribute may be altered before being written to the page. Removing this will mean they are left alone by .NET.
如果您不需要它,请runat="server"
从这些元素中删除该属性。具有此属性的控件在写入页面之前可能会被更改。删除这将意味着它们被 .NET 单独留下。
On a plain HTML element, removeAttribute
should work.
在普通的 HTML 元素上,removeAttribute
应该可以工作。
<input id="test" type="text" disabled="disabled" value="Some Value">
<script>
document.getElementById('test').removeAttribute('disabled');
</script>
If you View Source
on the page in the browser, you can check to see if .NET has adjusted something, for example the id attribute.
如果您View Source
在浏览器中的页面上,您可以检查 .NET 是否已调整某些内容,例如 id 属性。
I have tested removeAttribute
in the latest versions of Firefox and Internet Explorer.
我已经removeAttribute
在最新版本的 Firefox 和 Internet Explorer 中进行了测试。
回答by Jasmeen
This works for me!
这对我有用!
$('#test').prop('disabled', false);
$('#test').prop('disabled', false);