问题
I have the following Twitter Bootstrap Modal button (haml) - I want to use this button to save my form then open the modal dialog to ask if I want to print it. How Can I make this button perform a submit or save :
%a.btn{"data-toggle" => "modal", :href => "#myModal",
class: 'btn btn-inverse btn-large',
:role => "button"} Press to Complete Visit
#myModal.modal.hide.fade{"aria-hidden" => "true",
"aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}
回答1:
You should do an AJAX post and have an endpoint to receive the data.
HTML:
%a.btn{class: 'btn btn-inverse btn-large',
id: 'submit-btn',
:role => "button"} Press to Complete Visit
#myModal.modal.hide.fade{"aria-hidden" => "true",
"aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}
Javascript:
$('#submit-btn').click(function() {
$.ajax({
type: post,
url: '/your-end-point',
data: your_data,
success: function() {
$('#myModal').show();
}
})
})
Rails:
routes.rb
post 'your_end_point' => 'your_controller#submit'
your_controller.rb
def submit
# Save form data...
render :json => 'success'
end
来源:https://stackoverflow.com/questions/17265830/twitter-bootstrap-modal-submit-button-to-submit-and-open-modal-in-rails