如何禁用文本区域?
如何禁用动态添加到 HTML 的文本区域?
HTML:
<div class="ss_datesel_inp_cont">
<div class="ss_datesel_inp_right_corner">
</div>
<input autocomplete="off">
</div>
这是我尝试过的:
$('.ss_datesel_inp_cont:textarea').prop("disabled", true);
$('.ss_datesel_inp_cont:input').prop("disabled", true);
How to disable a textarea which is dynamically added to the HTML?
HTML:
<div class="ss_datesel_inp_cont">
<div class="ss_datesel_inp_right_corner">
</div>
<input autocomplete="off">
</div>
This is what I tried:
$('.ss_datesel_inp_cont:textarea').prop("disabled", true);
$('.ss_datesel_inp_cont:input').prop("disabled", true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在您发布的代码中没有看到 textarea 元素?根据您拥有的标记,您可以通过以下方式禁用输入:
您的代码无法工作的原因很简单,因为选择器是错误的。输入是
.ss_datesel_inp_cont
的后代,因此您需要在.ss_datesel_inp_cont
和input
之间使用空格来指示它。输入也是直接子代,因此您也可以使用直接子标记>
,如$('.ss_datesel_inp_cont > input')
中所示。I don't see a textarea element in the code you posted? Based on the markup you have, you would disable the input by:
The reason your code isn't working is simply because the selector is wrong. The input is a descendant of
.ss_datesel_inp_cont
and therefore you need to indicate that with a space between.ss_datesel_inp_cont
andinput
. The input is also a direct child, so alternatively you could use the direct child token>
as in$('.ss_datesel_inp_cont > input')
.要更改禁用的属性,您应该使用 .prop() 函数。
注意:对于 jQuery 1.6+
To change the disabled property you should use the .prop() function.
Note: For jQuery 1.6+
您是否尝试过,
prop
仅适用于 jQuery 1.6 或更高版本。Have you tried,
prop
only works for jQuery 1.6 or later.我会尝试:
或者
I would try:
or
您可以执行以下操作:
$('.ss_datesel_inp_cont input').attr('disabled','disabled');
您的
input
位于div,所以你可以像用css选择一样,通过一个空格来选择它。
它是
div
的子级,因此您也可以执行以下操作:.ss_datesel_inp_cont >输入
示例: http://jsfiddle.net/UpHsA/
You can do this:
$('.ss_datesel_inp_cont input').attr('disabled','disabled');
Your
input
is within thediv
, so you can select it in the same way as you select with css, by a space.It is a child of the
div
, so you could also do this:.ss_datesel_inp_cont > input
Example: http://jsfiddle.net/UpHsA/