如何在提交后清除消息而不刷新页面

时间:2015-08-19 01:11:03

标签: javascript

我有一个输入表单,用于提交消息。在我提交消息后,文本仍在文本区域中。如何在不重新加载页面的情况下提交测试区域?我需要javascript来做吗?

<form method="post" action="mess_add.php" target="list_mess">
<textarea cols="28" rows="3" name="text" maxlength="20000" onkeydown="if (event.keyCode == 13) document.getElementById('post').click(); myToken()">
</textarea>
<input type="submit" name="submit" value="Submit" id="post"/></form>

1 个答案:

答案 0 :(得分:0)

请参阅:How to clear text area with a button in html using javascript?

在这种情况下,你需要这样的东西:

document.querySelector('textarea[name=text]').value = '';

更新:

也许值得查看JavaScript的addEventListener

<script>
  var form = document.querySelector('form'); // Use an ID if there are multiple forms.
  var textarea = document.querySelector('textarea[name=text]');
  var button = document.querySelector('#post');

  // When the form is submitted, clear the textarea.
  form.addEventListener('submit', function () {
    setTimeout(function () {
      textarea.value = '';
    }, 100);
    return true; // Confirms that it's OK to continue submitting the form.
  });

  // When the return key is typed, submit the form.
  textarea.addEventListener('keypress', function (e) {
    if (e.keyCode === 13) {
      button.click();
      e.preventDefault();
    }
  });
</script>