如何在不刷新页面的情况下重置输入值

时间:2020-08-21 07:23:06

标签: javascript jquery

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

以下代码在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>

请提供代码

2 个答案:

答案 0 :(得分:3)

无需使用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>

答案 1 :(得分:1)

如果我对您的理解正确,那么您正在寻找以下内容:

function reset() {
    $(".comment").val('');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

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

快乐编码!

相关问题