繁体   English   中英

如何根据单选按钮切换启用/禁用输入字段

[英]How to toggle enable/disable input field based on radio button

我有一组单选按钮,它们带有lase选项“ Other”和一个输入field type text 默认情况下,输入字段是禁用的,如果单击“其他”单选按钮,则会启用该字段。 如果更改了单选按钮,如何禁用输入字段?

这是我的代码:

$(document).ready(function(){
     $("#text-inputt").prop('disabled', true)
 });
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});

如果更改单选,此代码不会禁用输入字段。

更新1我正在尝试:

 $(document).ready(function(){
     $("#text-inputt").prop('disabled', true);
 });
$("#lastt").click(function(){ 
if($(this).is(':checked')) {
      $("#text-inputt").prop("disabled", false);
  }  else {
      $("#text-inputt").prop("disabled", true);
  }  
});

但是,如果您更改单选按钮,则不会将输入设置为禁用。

变化:

$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});

 $(document).ready(function(){
 $("#text-inputt").prop('disabled', true)
 });
 $("#lastt").click(function(){
 if($(this).val=='Other')
 {
 $("#text-inputt").prop('disabled', false)
 }
 else
 {
  $("#text-inputt").prop('disabled', true)
 }
 });

试试这个,这里输入框将切换,即启用/禁用

$("#radioBtn").change(function(){
  $("#inputBox").attr("disabled", ! $(this).is(':checked'));    
});

你可以做这样的

 $(document).ready(function(){
    $("#text-inputt").prop('disabled', true);
    $('input[type=radio]').click(function(){
        if($(this).prop('id') == "lastt"){
         $("#text-inputt").prop("disabled", false);
      }else{
        $("#text-inputt").prop("disabled", true);
      }

    });
 });

解决方案可能是:

  • change侦听器添加到您的单选按钮组
  • 当收音机改变时,检查其值
  • 如果值为“ other”,则启用输入字段。

工作示例: https : //jsfiddle.net/3afjqe7j/1/

var $input = $('#text-inputt');

$("input[name=my-radios]").on('change', function() {
    var disabled = $(this).val() !== 'other';
    $input.prop('disabled', disabled);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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