Pick up radio value and pass to an Hidden input with same name

Asked

Viewed 902 times

0

I need to get the value of all radio input (that are checked) of my page, has around 10 radio, the value is loaded directly from the database, but they are disabled because the user can not change the value, they are already checked, I need to pass the value of these checkbox disabled to the Hidden input, because as you may already know, disabled inputs do not have the data sent to php.

Follows code: https://jsfiddle.net/h3ept1r5/1/

<div class="form-group">
  <input type="hidden" name="a">
  <div class="radio">
    <label for="yes1">
      <input type="radio" name="a" id="yes1" checked> Yes
    </label>
    <label for="no1">
      <input type="radio" name="a" id="no1"> No
    </label>
  </div>
</div>
<div class="form-group">
  <input type="hidden" name="b">
  <div class="radio">
    <label for="yes2">
      <input type="radio" name="b" id="yes2"> Yes
    </label>
    <label for="no2">
      <input type="radio" name="b" id="no2" checked> No
    </label>
  </div>
</div>
<div class="form-group">
  <input type="hidden" name="c">
  <div class="radio">
    <label for="yes3">
      <input type="radio" name="c" id="yes3" checked> Yes
    </label>
    <label for="no3">
      <input type="radio" name="c" id="no3"> No
    </label>
  </div>
</div>
<button class="btn btn-default">Submit</button>
  • 1

    If they already come from the server, IE, the server already "knows" which are selected, I did not understand the reason to take back the values.

2 answers

3

Instead of using the property disabled, do the event onclick returns false, so the normal action will not be executed and the user will not be able to uncheck the option.

With this solution, your field will be sent in the body of the request message.

<input type="radio" checked onclick="return false;" onkeydown="return false;"/>

There may be a problem, as the option is not disabled and only prevents the user from clicking, you can add the property disabled only in options that are unchecked:

<input type="radio" checked onclick="return false;" onkeydown="return false;"/> Sim
<input type="radio" disabled onclick="return false;" onkeydown="return false;"/> Não

See in operation: https://jsfiddle.net/h3ept1r5/3/

Another option is to use CSS and style input.

You can see in this topic why the above solution works: Why "Return false;" in a click event cancels the opening of the link?, although the question portrays a link, the explanation also serves for a radio button.

  • I have been thinking of answering in this way, but the question of the user’s experience would be impaired, since the appearance of the radio would be as enabled, giving the intention that there is possibility of behavior change, would have some suggestion to this question ?

  • thought about it too

  • @Leandro with CSS you can change the appearance and simulate a disabled.

  • @Gabrielrodrigues with CSS is possible.

  • @Leandro you can put the property disabled only in the option that is unchecked, so the user will know that he cannot select it and the active option will be well in view. See: https://jsfiddle.net/h3ept1r5/3/

  • @Filipemoraes got it, thank you!

Show 1 more comment

1


You can do each on your radio input by traversing and transferring the values to the Hidden input.

I used Parent() and sibligns to locate Hidden and insert the value.

Example:

$('input[type=radio]').attr('disabled', true);
$('input[type=radio]:checked').each(function() {
   $(this).parent().parent().siblings('input[type=hidden]').val(this.value);
});

See working on jsfiddle

Or as suggested by Filipe, add a css simulating the disabled and give a false Return.

Example:

$('input[type=radio]').css('opacity', '0.5');
$('input[type=radio]').on('click , keydown', function() {
  return false;
});

See working on jsfiddle

  • strange the first example did not work for me...

  • have to consider that it takes value only from radio that is checked

  • @Leandro probably this occurred because you want to take the value of an input, and your inputs have no values, see this example: https://jsfiddle.net/filadown/nqxv9hsa/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.