如果使用 jquery 检查单选按钮,则启用输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10498036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:25:37  来源:igfitidea点击:

enable input fields if radio buttons is checked with jquery

jqueryradio-button

提问by hyperrjas

I have this radio buttons in html code:

我在 html 代码中有这个单选按钮:

<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:

我有 1 个禁用字段,例如:

<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 Yesin radio buttons, remove the html attributedisabled="disabled"to this last input field, and if user click in option Noin radio buttons add the attributedisabled="disabled"to this last input field

我希望如果用户单击单选按钮中的选项Yes,则删除disabled="disabled"最后一个输入字段的 html 属性,如果用户单击单选按钮中的选项No,则将该属性添加disabled="disabled"到最后一个输入字段

How can I do it with jquery?

我怎样才能用 jquery 做到这一点?

thank you!

谢谢你!

回答by Ravi Gadag

you can use removeAttr

你可以使用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

JsFiddle 中查看演示

回答by Fabrizio Calderan

$('input[name="item[shipping]"]').on('click', function() {
   if ($(this).val() === 'true') {
      $('#item_shipping_cost').removeProp("disabled");
   }
   else {
      $('#item_shipping_cost').prop("disabled", "disabled");
   }
});

回答by Community Driven Business

You would like to use JavaScript like this:

你想像这样使用 JavaScript:

   $('#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 exampleof your case.

? 这是您案例的完整示例

回答by mesimplybj

Here is a

这里有一个

DEMO

演示

. 我想这会帮助你