[英]Rails 5 ajax form: submit form on clicking one of the 2 radio buttons in Ajax
Note: Similar questions exist but none of them resemble my case注意:存在类似的问题,但没有一个与我的情况相似
I am making a small Ajax form in my rails views with yes or no radio buttons, I made this form with remote: true
and it acts correctly as Ajax if I use a separate submit button (no page reload happens & form is passed to controller)我在我的 Rails 视图中使用是或否单选按钮制作了一个小的 Ajax 表单,我使用
remote: true
制作了这个表单,如果我使用单独的提交按钮,它就像 Ajax 一样正确(没有页面重新加载发生并且表单传递给控制器)
If I use Jquery to make the radio buttons themselves submit the form, then it submits but not in Ajax (but with a page reload)如果我使用 Jquery 使单选按钮本身提交表单,那么它会提交但不在 Ajax 中(但会重新加载页面)
So how to fix this to:那么如何解决这个问题:
Submit form on clicking one of the 2 radio buttons in Ajax?单击 Ajax 中的 2 个单选按钮之一提交表单?
The two things I tried:我尝试过的两件事:
1) Make two different submit buttons & try to adjust the form value in the submit btn itself, if I make them as submit buttons, Ajax works well but the form doesn't get my required value 1)制作两个不同的提交按钮并尝试在提交btn本身中调整表单值,如果我将它们作为提交按钮,Ajax 效果很好,但表单没有得到我需要的值
<div class="radio-button-btn-wrapper mr-2">
<%= f.submit t('yes'), recommended: true, class: "radio-button-btn-label-white background-green" %>
</div>
<div class="radio-button-btn-wrapper mr-2">
<%= f.submit t('no'), recommended: false, class: "radio-button-btn-label-white background-red" %>
</div>
2) Adjust the radio buttons to submit the form in Ajax manner (tried this but it didn't work) 2) 调整单选按钮,以 Ajax 方式提交表单(试过了,但没用)
<%= simple_form_for [...model names], remote: true do |f| %>
<div class="flex">
<div class="radio-button-btn-wrapper mr-2">
<%= label :recommended, t('yes'), class: "radio-button-btn-label white" %>
<%= f.radio_button :recommended, true, onclick: "$(this).closest('form').submit();", class: "radio-button-btn background-green" %>
</div>
<div class="radio-button-btn-wrapper">
<%= label :recommended, t('no'), class: "radio-button-btn-label white" %>
<%= f.radio_button :recommended, false, onclick: "$.ajax((this).closest('form').submit());", class: "radio-button-btn background-red" %>
</div>
</div>
What worked for me was adding a 'normal' submit button and set it to display none.对我有用的是添加一个“正常”提交按钮并将其设置为不显示。 Then add an eventListener that listen to a click on the radio button and if that happens, let it click on the hidden submit button.
然后添加一个监听单选按钮点击的事件监听器,如果发生这种情况,让它点击隐藏的提交按钮。
<%= simple_form_for [...model names], remote: true do |f| %>
<div class="flex">
<div class="radio-button-btn-wrapper mr-2">
<%= label :recommended, t('yes'), class: "radio-button-btn-label white" %>
<%= f.radio_button :recommended, true, class: "radio-button-btn background-green" %>
</div>
<div class="radio-button-btn-wrapper">
<%= label :recommended, t('no'), class: "radio-button-btn-label white" %>
<%= f.radio_button :recommended, false, class: "radio-button-btn background-red" %>
</div>
<%= f.submit '', class: 'd-none', id: 'submitMyForm' %>
</div>
document.addEventListener('click', function(event){
if(event.target.classList.contains('radio-button-btn'){
document.querySelector('#submitMyForm').click();
}
});
Please look into the code below and try to adapt it for your case.请查看下面的代码并尝试根据您的情况进行调整。 This is working code I have tested in my Rails app.
这是我在 Rails 应用程序中测试过的工作代码。
The basic idea is instead of trying to make the radio buttons as submit buttons I employed the approach of a hidden form and then bind click events on radio buttons.基本思想是,我没有尝试将单选按钮作为提交按钮,而是采用了隐藏表单的方法,然后在单选按钮上绑定点击事件。 In the button click handler I set the selected radio button's value as a hidden field value in hidden form and submit the hidden form.
在按钮单击处理程序中,我将所选单选按钮的值设置为隐藏表单中的隐藏字段值并提交隐藏表单。 It gets submitted as an AJAX request and on controller-end handle the JS response.
它作为 AJAX 请求提交,并在控制器端处理 JS 响应。
Hope this helps.希望这可以帮助。
routes.rb路线.rb
root "welcome#index"
post "welcome_submit_form" => "welcome#submit_form", as: :welcome_submit_form
app/controllers/welcome_controller.rb应用程序/控制器/welcome_controller.rb
class WelcomeController < ApplicationController
def index
end
def submit_form
@received_radio_value = params[:selected_radio_value]
render file: "welcome/submit_form_response.js.haml"
end
end
app/views/welcome/index.html.haml app/views/welcome/index.html.haml
.my-container<
%div(style='display: none;')
= form_tag(welcome_submit_form_path, class: 'my-hidden-form', remote: true) do |f|
= hidden_field_tag "selected_radio_value", nil
= radio_button_tag 'favorite_color', 'red', false, class: 'my-radio-btn'
Red
= radio_button_tag 'favorite_color', 'blue', false, class: 'my-radio-btn'
Blue
.my-selected-radio-value(style='display: none')
app/views/welcome/submit_form_response.html.haml app/views/welcome/submit_form_response.html.haml
%h2= "My Radio Value: #{@received_radio_value}"
app/views/welcome/submit_form_response.js.haml app/views/welcome/submit_form_response.js.haml
- response_markup = escape_javascript(render( partial: "welcome/submit_form_response.html.haml" ))
:plain
var responseMarkup = "#{response_markup}";
var responseContainer = $('.my-selected-radio-value');
responseContainer.html(responseMarkup);
responseContainer.show();
app/assets/javascripts/welcome.js应用程序/资产/javascripts/welcome.js
(function ($) {
bindClickEventOnMyRadioBtn = function() {
$('.my-container .my-radio-btn').off('click').on('click', function(event) {
var selectedRadioVal = $(this).val();
var myHiddenForm = $('.my-container .my-hidden-form');
var radioValueField = myHiddenForm.find('input[name=selected_radio_value]');
radioValueField.val(selectedRadioVal);
myHiddenForm.submit();
})
}
}) (jQuery);
var ready;
ready = function() {
bindClickEventOnMyRadioBtn();
};
$(document).ready(ready);
After extensive searching for why this happens & how to fix it in the easiest way, I found this issue in Rails 5.2 & thanks to this solution from Padi we should use this code to make it work as expected:在广泛搜索为什么会发生这种情况以及如何以最简单的方式解决它之后,我在 Rails 5.2 中发现了这个问题,并且感谢Padi 的这个解决方案,我们应该使用这个代码让它按预期工作:
// Get hold of the form object
form = document.querySelector('form');
// Use the Rails fire helper
Rails.fire(form, 'submit');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.