我试图让表单在提交后清除,但我发现的方法都没有。
HTML
<div id ="newcomment">
<%= form_for([@thing, @good_comment], remote: true) do |f| %>
<br>
<%= f.text_area :text, :placeholder => "leave a comment...", class: "newcommentform" %>
<div id="btn"><%= f.submit "Post", class: "btn btn-small", id: "postacomment" %></div>
<% end %>
</div>
的javascript
<script type="text/javascript">
$("#postacomment").click(function () {
$('.newcommentform').clearForm();
});
</script>
点击提交按钮没有做任何事情,在网络控制台中我收到以下错误:
TypeError: $(...).clearForm is not a function
我也尝试了其他常用方法。 $("#newcommentform')[0].reset();
返回错误
TypeError: $(...)[0] is undefined
并且$("#newcommentform.frm").trigger("reset");
没有返回错误,但也没有做任何其他事情。我错过了什么?
答案 0 :(得分:0)
试试这个:
$( '#newcommentform' ).each(function(){
this.reset();
});
答案 1 :(得分:0)
您的clearForm()似乎不存在,您是否创建了一个清除表单中所有输入和textarea的函数?因为clearForm()本身不是js函数。
您可以通过这种方式清除表单:
$("#postacomment").click(function() {
$(this).closest('form').find("input[type=text], textarea").val("");
});
答案 2 :(得分:0)
足以使用此代码:
$('.newcommentform')[0].reset()
因为$('.newcommentform')
返回一个数组
$("#newcommentform')[0].reset(); // on ' and "
// you must write
$("#newcommentform")[0].reset();
您知道在jQuery select中使用.
和whem #
的时间吗?如果.
正在使用类名$('.className')
且#
正在使用ID为$('#id')