【问题标题】:Toggle Show and Enable Textfiled When Certain Radio Button is Selected在选择某些单选按钮时切换显示和启用文本字段
【发布时间】:2013-08-31 20:50:06
【问题描述】:

我希望能够在每次选择“其他”单选按钮时显示和启用一个输入字段(最初隐藏和禁用),并且在选择不同的单选按钮时隐藏和禁用.我已经让这个脚本适当地显示和隐藏文本字段,但我想知道如何启用和禁用它

$('input[name="x_description"]').bind('change',function(){
    var showOrHide = ($(this).val() == "Other") ? true : false;
    $('#other_program_text').toggle(showOrHide);  
});

我知道这是如何显示字段并启用文本字段,但无法弄清楚如何将它们放在一起,

$(this).show().prop('disabled', false);

提前致谢, 丹

【问题讨论】:

  • 为什么需要禁用隐藏输入?
  • 所以不会和表单一起提交。

标签: javascript jquery toggle bind


【解决方案1】:
$('input[name="x_description"]').bind('change',function(){
    if ( showOrHide == true ) {
        $('#other_program_text').show().prop('disabled',false);
    } else if ( showOrHide == false ) {
        $('#other_program_text').hide().prop('disabled',true);
    }
});

【讨论】:

  • 嗨,这个解决方案似乎更接近。除了它有点越野车。似乎当我选择“其他”单选按钮并启用文本字段时。我可以单击页面上的任何其他位置,然后再次禁用文本字段。如果我选择不同的单选按钮,它应该只这样做。有什么想法或解决方案吗? cabrillo.edu/associations/foundation/how-give-online7.php
  • 实际上这个解决方案有效。我的代码有错误。非常感谢!
【解决方案2】:

您想使用 $(this).attr() 函数来设置 HTML 元素的属性。一个参数它会读取传递的参数,两个它会设置它。

点赞 $(this).attr("enabled", true)

【讨论】:

    【解决方案3】:

    您可以使用“点击”方法

     $('input[name="x_description"]').click(function() {
           if($('#radio_button').is(':checked')) { 
               alert("it's checked"); //do your stuff
            }
     });
    

    如果您的内容是动态生成的,则使用“实时”方法

     $('input[name="x_description"]').live('click',function() {
               if($('#radio_button').is(':checked')) { 
                   alert("it's checked"); //do your stuff
                }
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2018-07-23
      • 2012-12-28
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多