Twitter Bootstrap Modal submit button to submit and open modal in Rails

那年仲夏 提交于 2019-12-13 06:25:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!