javascript - Rails: Calling JS function on form_for radio buttons - Stack Overflow

I have a form_for with radio buttons, and I want to call a javascript function when the radio button is

I have a form_for with radio buttons, and I want to call a javascript function when the radio button is changed.

The radio buttons are for Chapter's field 'type', of type boolean.

<%= form_for([book.category, book, chapter], remote: true ) do |f| %>

    <%= f.radio_button :type, true, onKeyUp: "alert('hi')", checked: 'checked' %> 
    <%= f.label :type, 'Link' %>

    <%= f.radio_button :type, false, onKeyUp: "alert('hi')" %> 
    <%= f.label :type, 'Text' %>

<% end %>

I've tried onchange and onclick as well. I've tried adding a {} before the options, and I get an error from Rails that there are too many parameters.

Eventually I want to call a function that hides/shows other text fields, but I can't even get the alert to execute!

I'm sure this is something really simple, but I've been searching the internet for the answer to this for way too long. Thanks for your help!

I have a form_for with radio buttons, and I want to call a javascript function when the radio button is changed.

The radio buttons are for Chapter's field 'type', of type boolean.

<%= form_for([book.category, book, chapter], remote: true ) do |f| %>

    <%= f.radio_button :type, true, onKeyUp: "alert('hi')", checked: 'checked' %> 
    <%= f.label :type, 'Link' %>

    <%= f.radio_button :type, false, onKeyUp: "alert('hi')" %> 
    <%= f.label :type, 'Text' %>

<% end %>

I've tried onchange and onclick as well. I've tried adding a {} before the options, and I get an error from Rails that there are too many parameters.

Eventually I want to call a function that hides/shows other text fields, but I can't even get the alert to execute!

I'm sure this is something really simple, but I've been searching the internet for the answer to this for way too long. Thanks for your help!

Share Improve this question asked Jul 4, 2014 at 21:19 user3234309user3234309 1792 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

this code actually works with an onclick event, inspect the generated HTML code with a developer extension like Firebug in your browser to make sure the attributes have been set correctly.

<%= form_for([book.category, book, chapter], remote: true ) do |f| %>

    <%= f.radio_button :type, true, checked: 'checked', onClick: "alert('hi TRUE!');" %> 
    <%= f.label :type, 'Link' %>

    <%= f.radio_button :type, false, onClick: "alert('hi False!');" %> 
    <%= f.label :type, 'Text' %>

<% end %>

Another way is to define a behavior using a data tag and then bind it with javascript (reference: http://blog.bigbinary./2012/10/10/data-behavior.html):

<%= form_for([book.category, book, chapter], remote: true ) do |f| %>

    <%= f.radio_button :type, true, checked: 'checked', data: {behavior: "clickable"} %> 
    <%= f.label :type, 'Link' %>

    <%= f.radio_button :type, false, data: {behavior: "clickable" } %> 
    <%= f.label :type, 'Text' %>

<% end %>

JavaScript:

$(document).ready(function() {    
  $('input[type="radio"][data-behavior="clickable"]').click(function(evt) {
    alert("you chose the option: " + $(this).val());
  });
});

Let's extend this to your scenario for hiding/showing other stuff:

View ERB:

<%= form_for([book.category, book, chapter], remote: true ) do |f| %>

    <%= f.radio_button :type, true, checked: 'checked', data: {behavior: "toggle-products"} %> 
    <%= f.label :type, 'Link' %>

    <%= f.radio_button :type, false, data: {behavior: "toggle-products" } %> 
    <%= f.label :type, 'Text' %>

<% end %>

JavaScript:

$(document).ready(function() {
  $('input[type="radio"][data-behavior="toggle-products"]').click(function(evt) {
    if($(this).val() == "true") { 
      $('.products').show();
    } else {
      $('.products').hide();
    }
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196731a4616129.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信