簡體   English   中英

表單提交后清除文本框

[英]clear textbox after form submit

我有一個使用ajax發布的表單並重新加載下面的div。

按下提交按鈕時,我需要清除文本框。

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

textarea添加id

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

掛鈎提交過程並添加:

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

如果你沒有使用jQuery(這是上面給出的解決方案所必需的),你可以替換

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

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

發布后簡單地稱之為:

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

或者要改進,為您的textarea分配一個ID:

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

並在發布后使​​用此:

$("#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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM