我有一个Rails表单。但我不想通过Rails提交表单。我想通过Ajax提交它。
在视图文件
中 <li><%= submit_tag 'Save', id: 'submit', :class => 'button' %></li>
在js文件中
$(".submit").click(function() {
alert("hi");
event.preventDefault();
health_marks_array = form_hash_and_submit();
if (0 < health_marks_array.length) {
call_form_submit(health_marks_array);
}
else{
location.href = '/thinking_health_card/health_marks/new?form_name=' + form_name + '&student_id=' + student_id + '&health_exam_id=' + health_exam_id
}
return false
});
也试过
$(".submit").click(function(event) {
alert("hi");
event.preventDefault();
health_marks_array = form_hash_and_submit();
if (0 < health_marks_array.length) {
call_form_submit(health_marks_array);
}
else{
location.href = '/thinking_health_card/health_marks/new?form_name=' + form_name + '&student_id=' + student_id + '&health_exam_id=' + health_exam_id
}
return false
});
为什么它不起作用?
答案 0 :(得分:3)
从
改变$(".submit").click(function() {});
到
$("#submit").click(function(event) {
event.preventDefault();
....
});
元素的标识为#submit
,而非.submit