javascript - jQuery selected value always 0 -


i'm using laravel jquery webapplication. when submit empty form, redirect page old values filled in (form model binding), works expected.

the problem @ jquery part. have several sections not shown when value of radio group 0 (unchecked), , showed when changes 1 (checked). works fine when fill in page, bugs when redirect if have validation error in form.

when have form error, , redirected page old values filled in, , read selected values of radio groups, values still 0. why val() 0 when radio has selected value of 1?

var r4 = $('input[name="configtype"]'); var s4 = $('#comprehensive');  initsec(r4, s4);  function initsec(radio, togglesection){      console.log(radio.val());      //when being redirected page radio selected @ value '1',     //console.log still shows 0      if (radio.val() == 1)         {             togglesection.toggle();         }     } 

edit

var r4 = $('input[name="configtype"]:checked'); can't work in situation because i'm using radio field in other places in code shouldn't checked value instead of checked value, not duplicate far know (because i've read var r4 = $('input[name="configtype"]:checked'); million times that's not possible in code)

radio set of radio elements not checked 1 calling .val() return value of first element in set.

you can use .filter() select checked item

console.log(radio.filter(':checked').val()); 

or

var r4 = $('input[name="configtype"]:checked'); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -