admin管理员组

文章数量:1188429

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 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";
    }
}

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 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";
    }
}
Share Improve this question edited Feb 24, 2021 at 23:08 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Nov 30, 2012 at 2:09 Pi HorsePi Horse 2,4309 gold badges31 silver badges52 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

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

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

and JavaScript

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

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

<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>

Use jquery

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

or by Id

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

本文标签: javascriptEnabledisable textarea after taking input from keyboardStack Overflow