简体   繁体   English

仅在选中单选按钮时运行 javascript

[英]Run javascript only if radio button is checked

I want something quite simple for my Rails app: a javascript must be run only if one of the previous radio buttons was checked.我想为我的 Rails 应用程序提供一些非常简单的东西:只有在选中了之前的单选按钮之一时,才必须运行 javascript。 What I want exactly is that the user has different radio buttons shown to him, with no default checked.我真正想要的是用户有不同的单选按钮显示给他,没有检查默认值。 When he clicks on a "Run" button, below the radio buttons, two cases:当他单击单选按钮下方的“运行”按钮时,有两种情况:

  • the user checked a radio button, then the javascript is run (a div is hidden, and another one is shown)用户选中了一个单选按钮,然后运行 javascript(隐藏了一个 div,并显示了另一个)
  • the user didn't check a radio button, nothing happens (even more: the browser tells the user that he must checks a radio button, but that is easily done with a :required => true option in the radio button)用户没有检查单选按钮,没有任何反应(甚至更多:浏览器告诉用户他必须检查单选按钮,但这很容易通过单选按钮中的:required => true选项完成)

The code I tried:我试过的代码:

HTML.erb code HTML.erb 代码

<%= form_with(model: @pdfrequest, local: true, html: {class: "form-group"}) do |f| %>

    <%= f.radio_button :my_param, 'option1' %>
    <%= label :opt1, 'option1', :value => 'option1'%>

    <%= f.radio_button :my_param, 'option2' %>
    <%= label :opt2, 'option2', :value => 'option2'%>

    <div class="row mb-3">
        <div class="col", id="to_hide">
            <%= submit_tag "Run", id: "upload_button" %>
        </div>
    </div>

    <div id="to_show">
        <p>Show me!</p>
    </div>

<% end %>

I only managed to get the code working on click, no matter if the form is filled or not.无论表格是否填写,我只设法让代码在点击时工作。

jquery code jquery代码

$(document).on("click", "#upload_button", function() {
    $("#to_show").show();
});

$(document).on("click", "#upload_button", function() {
    $("#to_hide").hide();
});

The jquery code is located in my application.js in the assets folder. jquery 代码位于资产文件夹中的我的 application.js 中。

I would expect something like this, located in the same file as the html code I provided:我希望这样的内容与我提供的 html 代码位于同一文件中:

Expected answer预期答案

<% if form_with.filled %>
    $(document).on("click", "#upload_button", function() {
        $("#to_show").show();
    });

    $(document).on("click", "#upload_button", function() {
        $("#to_hide").hide();
    });
<% end %>

I found some other questions closely related to this issue, but every time, the condition was a "simple" condition on an object, but not linked to the form itself.我发现了一些与此问题密切相关的其他问题,但每次,条件都是 object 上的“简单”条件,但与表单本身无关。

I'm using Rails 5.2.3 and jquery 3.2.1我正在使用 Rails 5.2.3 和 jquery 3.2.1

Thanks.谢谢。

EDIT编辑

Thanks to this question: Find out whether radio button is checked with JQuery?感谢这个问题: 找出是否使用 JQuery 检查了单选按钮? , I made some progress. ,我取得了一些进展。

HTML.erb code HTML.erb 代码

<%= form_with(model: @pdfrequest, local: true, html: {class: "form-group"}) do |f| %>

    <%= f.radio_button :my_param, 'option1', :id => 'button_check' %>
    <%= label :opt1, 'option1', :value => 'option1'%>

    <%= f.radio_button :my_param, 'option2', :id => 'button_check' %>
    <%= label :opt2, 'option2', :value => 'option2'%>

    <div class="row mb-3">
        <div class="col", id="to_hide">
            <%= submit_tag "Run", id: "upload_button" %>
        </div>
    </div>

    <div id="to_show">
        <p>Show me!</p>
    </div>

<% end %>

jquery code jquery代码

$(document).on("click", "#upload_button", function() {
    if($('#button_check').is(':checked')) {
        $("#to_show").show();
    }
});

$(document).on("click", "#upload_button", function() {
    if($('#button_check').is(':checked')) {
        $("#to_hide").hide();
    }
});

So now, it doesn't execute the jquery when no button is checked, but even if a radio button is checked, it's not executed but I couldn't figure out what I did wrong.所以现在,当没有选中任何按钮时,它不会执行 jquery,但即使选中了单选按钮,它也不会执行,但我不知道我做错了什么。

Your second attempt is closer, but in order to execute both radio buttons would have to be checked since they have the same id.您的第二次尝试更接近,但为了执行两个单选按钮,必须检查它们,因为它们具有相同的 id。 Try adding a name field and using this:尝试添加一个名称字段并使用它:

if($("input:radio[name='name_here']").is(":checked")){

}

This will return FALSE if all the items in the inputs are unchecked and TRUE if an item is checked.如果输入中的所有项目都未选中,这将返回 FALSE,如果选中一个项目,则返回 TRUE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM