Javascript 从键盘输入后启用/禁用文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13638010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:33:35  来源:igfitidea点击:

enable/disable textarea after taking input from keyboard

javascripthtmljavascript-events

提问by Pi Horse

I have a textarea which is disabled by default. And then on press of 'Edit' I take some input from user. If it is valid, I want to enable the textarea. Here is the code which I have right now:

我有一个默认禁用的 textarea。然后在按下“编辑”时,我会从用户那里获取一些输入。如果有效,我想启用textarea。这是我现在拥有的代码:

<textarea name="comment" cols="5" rows="2" disabled="true"><%= $tmp_com %></textarea>
<a href="javascript:validateUser()">Edit</a>

function validateUser(){
var name=prompt("Please enter the password");

    if (name=="1234")
    {
       document.getElementByName("comment").disabled="false";
    }
}

回答by epascarello

There is no getElementByName in JavaScript. Easiest solution, add an id, and use getElementById.

JavaScript 中没有 getElementByName。最简单的解决方案,添加一个 id,然后使用 getElementById。

<textarea name="comment" id="comment" cols="5" rows="2" disabled="true">

and JavaScript

和 JavaScript

document.getElementById("comment").disabled="false";

回答by Akhil Sekharan

Its better for you to use id instead of name. Any way I'm using name here to follow the question.

最好使用 id 而不是 name。无论如何,我在这里使用名称来关注这个问题。

<a href="javascript:validateUser()">Edit</a>
<textarea name="comment" cols="5" rows="2" disabled="disabled">aaaaa</textarea>

<script type="text/javascript">
    function validateUser(){
        var name=prompt("Please enter the password");
        if (name=="1234")
        document.getElementsByName("comment")[0].disabled=false;
    }
</script>

回答by jeremy

Use jquery

使用jQuery

$("[name='comment']").attr('disabled', true);
$("[name='comment']").attr('disabled', false);

or by Id

或通过 ID

$("#comment").attr('disabled', true);