我如何在 form_tag rails4 中使用重置按钮
How can i use reset button in form_tag rails4
<%= form_tag do %>
<div class="container">
<div class="row">
<div class="input-field col s12">
<%= text_field_tag :sid , params[:sid], :length => 9 %>
<h6 class="grey-text" style="font-size: 1vw">Ex.570510XXX</h6>
<%= label_tag(:sid, "Student ID") %>
</div>
<div class="input-field col s6">
<%= text_field_tag :semester, params[:semester], :length => 1 %>
<%= label_tag(:semester, "Semester") %>
<h6 class="grey-text" style="font-size: 1vw">Ex.Term 2 = input 2</h6>
</div>
<div class="input-field col s6">
<%= text_field_tag :year, params[:year], :length => 2 %>
<%= label_tag(:year, "Year") %>
<h6 class="grey-text" style="font-size: 1vw">Ex.2558 = input 58</h6>
</div>
<br><br><br><br><br><br><br><br><br><br><br>
<div class="col s6">
<%= button_tag(value: 'Submit',class: "waves-effect waves-light btn right") do %>
Submit
<i class="material-icons right">send</i>
<% end %>
</div>
<div class="col s6">
<%= button_tag(value: 'CLEAR',type: 'reset',class: "waves-effect waves-light btn") do %>
CLEAR
<i class="material-icons right">delete</i>
<% end %>
</div>
</div>
我想要 button_tag type=reset 来清除每一页
当我在提交前重置时它的工作。如果我按提交它不工作。
试试我的页面:http://timetable4cmu.herokuapp.com/
输入:StudentID = 570510629,学期=2,年级=58
当您输入并提交时,清除按钮将不起作用。
请帮帮我!!
如果提交后不想要表单中的参数,则不应该在表单字段中传递参数。
来自这里:
<%= text_field_tag :sid , params[:sid], :length => 9 %>
<%= text_field_tag :semester, params[:semester], :length => 1 %>
<%= text_field_tag :year, params[:year], :length => 2 %>
收件人:
<%= text_field_tag :sid , '', :length => 9 %>
<%= text_field_tag :semester, '', :length => 1 %>
<%= text_field_tag :year, '', :length => 2 %>
是的,你的表单 reset
按钮在提交后不起作用,我认为(不确定)因为它无法重置默认值(提交后你会显示带有填充值的表单)。您可以使用 js/jquery
手动更新表单数据(使输入文本数据 ''
为空字符串)
function resetForm(){
$('input[ name=sid]').val('');
$('input[ name=semester]').val('');
$('input[ name=year]').val('');
}
并在 jquery
之后调用此方法
$('button[type=reset]').click(function(event){
event.preventDefault();
resetForm();
});
<%= form_tag do %>
<div class="container">
<div class="row">
<div class="input-field col s12">
<%= text_field_tag :sid , params[:sid], :length => 9 %>
<h6 class="grey-text" style="font-size: 1vw">Ex.570510XXX</h6>
<%= label_tag(:sid, "Student ID") %>
</div>
<div class="input-field col s6">
<%= text_field_tag :semester, params[:semester], :length => 1 %>
<%= label_tag(:semester, "Semester") %>
<h6 class="grey-text" style="font-size: 1vw">Ex.Term 2 = input 2</h6>
</div>
<div class="input-field col s6">
<%= text_field_tag :year, params[:year], :length => 2 %>
<%= label_tag(:year, "Year") %>
<h6 class="grey-text" style="font-size: 1vw">Ex.2558 = input 58</h6>
</div>
<br><br><br><br><br><br><br><br><br><br><br>
<div class="col s6">
<%= button_tag(value: 'Submit',class: "waves-effect waves-light btn right") do %>
Submit
<i class="material-icons right">send</i>
<% end %>
</div>
<div class="col s6">
<%= button_tag(value: 'CLEAR',type: 'reset',class: "waves-effect waves-light btn") do %>
CLEAR
<i class="material-icons right">delete</i>
<% end %>
</div>
</div>
我想要 button_tag type=reset 来清除每一页
当我在提交前重置时它的工作。如果我按提交它不工作。
试试我的页面:http://timetable4cmu.herokuapp.com/
输入:StudentID = 570510629,学期=2,年级=58 当您输入并提交时,清除按钮将不起作用。
请帮帮我!!
如果提交后不想要表单中的参数,则不应该在表单字段中传递参数。
来自这里:
<%= text_field_tag :sid , params[:sid], :length => 9 %>
<%= text_field_tag :semester, params[:semester], :length => 1 %>
<%= text_field_tag :year, params[:year], :length => 2 %>
收件人:
<%= text_field_tag :sid , '', :length => 9 %>
<%= text_field_tag :semester, '', :length => 1 %>
<%= text_field_tag :year, '', :length => 2 %>
是的,你的表单 reset
按钮在提交后不起作用,我认为(不确定)因为它无法重置默认值(提交后你会显示带有填充值的表单)。您可以使用 js/jquery
手动更新表单数据(使输入文本数据 ''
为空字符串)
function resetForm(){
$('input[ name=sid]').val('');
$('input[ name=semester]').val('');
$('input[ name=year]').val('');
}
并在 jquery
之后调用此方法$('button[type=reset]').click(function(event){
event.preventDefault();
resetForm();
});