admin管理员组

文章数量:1191725

I have a form which posts using ajax and reloads the div underneath.

I need the textbox to clear when the submit button is pressed.

<form name=goform action="" method=post>
<textarea name=comment></textarea>
<input type=submit value=submit>
</form>

I have a form which posts using ajax and reloads the div underneath.

I need the textbox to clear when the submit button is pressed.

<form name=goform action="" method=post>
<textarea name=comment></textarea>
<input type=submit value=submit>
</form>
Share Improve this question asked Dec 5, 2009 at 12:13 mrpatgmrpatg 10.1k44 gold badges113 silver badges169 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 20

Add an id for the textarea.

<textarea name='comment' id='comment'></textarea>

Hook into the submit process and add:

$('#comment').val('');

If you're not using jQuery (which is required for the solutions given above) you can replace the

$("#txtComment").val("");

with

document.getElementById("txtComment").value = "";

Simply call this after posting:

$("textarea[name=comment]").val("");

Or to improve, assign an ID to your textarea:

<form name=goform action="" method=post>
<textarea id="txtComment" name="comment"></textarea>
<input type=submit value=submit>
</form>

and use this after posting:

$("#txtComment").val("");
<script>
    function testSubmit()
    {
        var x = document.forms["myForm"]["input1"];
        var y = document.forms["myForm"]["input2"];
        if (x.value === "")
        {
            alert(' fill!!');
            return false;
        } Blockquote
        if(y.value === "")
        {
            alert('plz fill the!!');
            return false;
        }
        return true;
    }
    function submitForm()
    {
        if (testSubmit())
        {
            document.forms["myForm"].submit(); //first submit
            document.forms["myForm"].reset(); //and then reset the form values
        }
    } </script> <body>
    <form method="get" name="myForm">

        First Name: <input type="text" name="input1"/>
        <br/>
        Last Name: <input type="text" name="input2"/>
        <br/>
        <input type="button" value="Submit" onclick="submitForm()"/>
    </form>

</body>

本文标签: javascriptclear textbox after form submitStack Overflow