jQuery 如何禁用文本区域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7694694/
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
How to disable a text area?
提问by jQuerybeast
How to disable a textarea which is dynamically added to the HTML?
如何禁用动态添加到 HTML 的文本区域?
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);
回答by jbyrd
I don't see a textarea element in the code you posted? Based on the markup you have, you would disable the input by:
我在您发布的代码中没有看到 textarea 元素?根据您拥有的标记,您可以通过以下方式禁用输入:
$('.ss_datesel_inp_cont input').prop('disabled', true);
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
and input
. The input is also a direct child, so alternatively you could use the direct child token >
as in $('.ss_datesel_inp_cont > input')
.
您的代码不起作用的原因仅仅是因为选择器错误。输入是 的后代,.ss_datesel_inp_cont
因此您需要在.ss_datesel_inp_cont
和之间用空格表示input
。输入也是直接子代,因此您也可以使用直接子代标记,>
如$('.ss_datesel_inp_cont > input')
。
回答by Foreever
To change the disabled property you should use the .prop() function.
要更改禁用属性,您应该使用 .prop() 函数。
$("textarea").prop('disabled', true);
$("textarea").prop('disabled', false);
Note: For jQuery 1.6+
注意:对于 jQuery 1.6+
回答by aziz punjani
Have you tried,
你有没有尝试过,
$('.ss_datesel_inp_cont:textarea').attr("disabled", true);
prop
only works for jQuery 1.6 or later.
prop
仅适用于 jQuery 1.6 或更高版本。
回答by Matt Williamson
I would try:
我会尝试:
$('.ss_datesel_inp_cont:textarea').prop("disabled", "disabled");
or
或者
$('.ss_datesel_inp_cont textarea').prop("disabled", "disabled");
回答by Jason Gennaro
You can do this:
你可以这样做:
$('.ss_datesel_inp_cont input').attr('disabled','disabled');
$('.ss_datesel_inp_cont input').attr('disabled','disabled');
Your input
is within the div
, so you can select it in the same way as you select with css, by a space.
您input
在 中div
,因此您可以按照与使用 css 选择相同的方式通过空格选择它。
It is a child of the div
, so you could also do this:
它是 的孩子div
,所以你也可以这样做:
.ss_datesel_inp_cont > input
.ss_datesel_inp_cont > input
Example:http://jsfiddle.net/UpHsA/