javascript - 根据单选按钮选择显示/隐藏内容

标签 javascript jquery html css

我正在尝试根据单选按钮的选择来切换内容 DIV。

单选按钮的 HTML。

<div class="row-fluid">
    <label class="radio">
      <input type="radio" name="account" id="yes" value="yes" checked>
      Yes, I have an existing account
    </label>
    <label class="radio">
      <input type="radio" name="account" id="no" value="no">
      No, I don't have an account
    </label>
</div>

内容分区

<div id="account_contents">
    <p>This is account contents.......</p>  
</div>

这就是我在 jquery 中尝试的方式。

$('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});

但它不适合我。在这里,我只想在用户没有帐户时显示此内容 DIV。

谁能告诉我如何解决这个问题?

最佳答案

似乎你需要 .on('change') 单选按钮,而不仅仅是其中一个

$('input[type="radio"][name="account"]').on('change',function(){
  var ThisIt = $(this);
  if(ThisIt.val() == "yes"){
       // when user select yes
       $('#account_contents').fadeOut();
  }else{
       // when user select no
       $('#account_contents').fadeIn();
       $('#account_contents').find("input").val("");
       $('#account_contents').find('select option:first').prop('selected',true);
  }
});

Working Example

关于javascript - 根据单选按钮选择显示/隐藏内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469807/

相关文章:

javascript - ng-repeat 按现有数组值过滤

javascript - 如何在 jquery mobile 中显示之前设置页面中选择框的值?

javascript - 使用chrome扩展在knockout js页面上触发(表单)更改事件

jQuery:突出显示偶数列(不是行)

javascript - 基本 require.js 示例

javascript - getElementById - Javascript

php - 真正的随机数

javascript - ES6 从继承类调用父函数

jquery - 使用 jQuery 删除只有一个空格的元素

node.js - textarea 和 input HTML 元素之间的安全差异?