enable input fields if radio buttons is checked with jquery
Asked Answered
E

4

6

I have this radio buttons in html code:

<span><input type="radio" value="false" name="item[shipping]" id="item_shipping_false" checked="checked"><label for="item_shipping_false" class="collection_radio_buttons">No</label></span>

<span><input type="radio" value="true" name="item[shipping]" id="item_shipping_true"><label for="item_shipping_true" class="collection_radio_buttons">Yes</label></span>

and I have 1 disabled field like:

<input id="item_shipping_cost" class="currency optional" type="text" size="30" name="item[shipping_cost]" disabled="disabled">

I want that if an user click in option Yes in radio buttons, remove the html attributedisabled="disabled" to this last input field, and if user click in option No in radio buttons add the attributedisabled="disabled" to this last input field

How can I do it with jquery?

thank you!

Elfreda answered 8/5, 2012 at 11:51 Comment(0)
K
8

you can use removeAttr

   $('#item_shipping_true').click(function()
{
  $('#item_shipping_cost').removeAttr("disabled");
});

$('#item_shipping_false').click(function()
{
  $('#item_shipping_cost').attr("disabled","disabled");
});

see demo in JsFiddle

Kassandrakassaraba answered 8/5, 2012 at 11:59 Comment(0)
C
6
$('input[name="item[shipping]"]').on('click', function() {
   if ($(this).val() === 'true') {
      $('#item_shipping_cost').removeProp("disabled");
   }
   else {
      $('#item_shipping_cost').prop("disabled", "disabled");
   }
});
Calen answered 8/5, 2012 at 12:0 Comment(0)
P
3

You would like to use JavaScript like this:

   $('#item_shipping_false').change(function() {
      $('#item_shipping_cost').prop('disabled', false);
    });
    $('#item_shipping_true').change(function() {
      $('#item_shipping_cost').prop('disabled', true);
    });

​ This is complete example of your case.

Piling answered 8/5, 2012 at 12:0 Comment(0)
U
1

Here is a

DEMO

. I think this will help you
Ultrasound answered 8/5, 2012 at 12:7 Comment(1)
the input not disable when click noEaddy

© 2022 - 2024 — McMap. All rights reserved.