首页 > 解决方案 > 如何在不刷新页面的情况下重置输入值

问题描述

我想在单击发布按钮后重置输入值而不刷新页面

以下代码在while循环中

<input class="comment" type="text" placeholder="Write comment"><button class="comment_post" onClick="reset()">Post</button>

我用过

function reset(){
    document.getElementsByClassName('comment').value=null;
}
<input class="comment" type="text" placeholder="Write comment"><button class="comment_post" onClick="reset()">Post</button>

请提供代码

标签: javascriptjquery

解决方案


不使用 jQuery 文件包含,您可以简单地使用以下函数。

function reset()
{
  document.getElementsByClassName('comment')[0].value='';
}
<input class="comment" type="text" placeholder="Write comment">
<br><br>
<button class="comment_post" onClick="reset()">Post</button>


推荐阅读