如何使用这个简单的 jQuery 禁用 HTML 文本字段?
我有这个简单的 jQuery 代码来测试。
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("text").attr("disabled","");
});
});
</script>
</head>
<body>
<input type="text">
<br />
<button>Set the textfield disabled</button>
</body>
</html>
基本上,HTML 页面带有一个简单的按钮和文本字段。我只想在单击按钮时禁用输入字段。但不起作用???
(PS:这段代码来源于w3schools.com网站,只是为了简单测试一下jQuery有多强大)
I have this simple jQuery code to test out.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("text").attr("disabled","");
});
});
</script>
</head>
<body>
<input type="text">
<br />
<button>Set the textfield disabled</button>
</body>
</html>
Basically the HTML page comes with a simple button and textfield. All I want to have the input field disabled as I click the button. But it doesn't work???
(PS: this code is sourced out from w3schools.com website, just to simply test out how powerful jQuery is)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 jQuery 1.7 开始,您可以使用
.prop
:在 1.7 之前,您可以这样做:
PS:使用
$(":text")
或$('input[ type="text"]')
选择文本类型的所有元素。From jQuery 1.7, you could use
.prop
:Before 1.7, you could do:
PS: Use
$(":text")
or$('input[type="text"]')
to select all elements of type text.试试这个:
Try this:
或者(更现代):
Or (more modern):
jquery
中没有text
选择器。您需要使用属性选择器[attribute=value]
或:
有一个
:text
选择器,但它的效率不如第一个选项,请参阅 文档:请注意,您的 XHTML 无效。您应该关闭
=>
<输入类型=“文本”/>
There is not
text
selector injquery
. You need to use the attribute selector[attribute=value]
or:
there is a
:text
selector but it's less efficent then the first option, see the docs:Note that your's XHTML isn't valid. You should close the
<input type="text">
=><input type="text" />
$("输入").attr("已禁用","已禁用");
$("input").attr("disabled","disabled");
“disabled”是一个属性,而不是属性本身。像“checked”或“disabled”这样的布尔值在以这种方式访问时并不总是能正确更新(或检索)。 代替使用
。
"disabled" is a property, not an attribute per-se. Booleans like "checked" or "disabled" don't always get updated (or retrieved) properly when accessing them that way. Use
instead.