简体   繁体   中英

Dynamic form's javascript not working when removed { } from select tag-Rails

I have a form with a gender field in rails as:

<%= f.select :gender, ["Male","Female"],{class: "gender"} %>  

i also tried it with this:

<%= f.select :gender, ["Male","Female"],{class: "gender"},onclick: "categorychange" %>

But it's not working. Now when I Tried:

<%= f.select :gender, ["Male","Female"],{},{class: "gender"} %>  

That's working all fine. which I found here. 在此处输入图片说明
I didn't get what the { } stands for.

and for different gender I have different corresponding field to show which are as follows:

<div class="male">
//male fields here//
</div>  

<div class="female">
//female fields here//
</div>  

I have the javascript as:

function categorychange(){
var val = $(this).val();
var gender = $(this).parent().find(".gender")
if (gender.val() == 'Male')
{
  $(this).parent().find('.male').show();  
  $(this).parent().find('.female').hide();
}
else if (gender.val()=='Female')
{
 $(this).parent().find('.male').hide();  
 $(this).parent().find('.female').show();
}  

But the javascript is not working. It shows both the div's.

var select_gender = $('.gender')
select_gender.change(function(){
if (select_gender.val() == 'Male')
{
  $('.male').show();  
  $('.female').hide();
}
else if (select_gender.val()=='Female')
{
 $('.male').hide();  
 $('.female').show();
}}

Rather than using onclick on the view, try onchange event in the javascript for the select input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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