使用外部按钮提交表单
Submit form using external button
在表单外部的视图中,我有一个股票 "Update" 按钮。单击按钮时没有任何反应。请帮忙。非常感谢。
(haml)
%button#update_button(type="submit) Update
The call for the form:
= form_for @employee, id: 'employee-update-form' do |f|
...
And the JS intended to submit the form:
:javascript
$('#update_button').click(function() {
$('#employee-update-form').submit(function() {
$.ajax({
type: "PATCH",
url: '#{employee_path(@employee)}'
data: $('#employee-update-form').serialize(),
dataType: "JSON"
}).success(function(json) {
});
return false; // prevents normal behavior
});
在表单上调用提交方法
$('#employee-update-form').submit();
您已将 ajax 请求绑定到表单提交事件,您仍需要调用 submit()
来触发该事件。
参见 jsfiddle:http://jsfiddle.net/ma9kLm34/
绑定调用后触发提交。
$('#employee-update-form').submit();
在表单外部的视图中,我有一个股票 "Update" 按钮。单击按钮时没有任何反应。请帮忙。非常感谢。
(haml)
%button#update_button(type="submit) Update
The call for the form:
= form_for @employee, id: 'employee-update-form' do |f|
...
And the JS intended to submit the form:
:javascript
$('#update_button').click(function() {
$('#employee-update-form').submit(function() {
$.ajax({
type: "PATCH",
url: '#{employee_path(@employee)}'
data: $('#employee-update-form').serialize(),
dataType: "JSON"
}).success(function(json) {
});
return false; // prevents normal behavior
});
在表单上调用提交方法
$('#employee-update-form').submit();
您已将 ajax 请求绑定到表单提交事件,您仍需要调用 submit()
来触发该事件。
参见 jsfiddle:http://jsfiddle.net/ma9kLm34/
绑定调用后触发提交。
$('#employee-update-form').submit();