简体   繁体   English

根据单选按钮值显示或隐藏表格行

[英]Show or hide a table row based on a Radio button value

I have this simple jQuery code below that shows or hides a bunch of table rows depending on the value of 2 radio buttons. 我下面有这个简单的jQuery代码,它根据2个单选按钮的值显示或隐藏一堆表行。

The code below works great for the click event but I would also like to add in code that work when the page is loaded, before the click event ever happens it should show or hide my table rows based on the value of the radio button. 下面的代码对于click事件非常有用,但是我也想添加在页面加载时起作用的代码,在click事件发生之前,它应该根据单选按钮的值显示或隐藏我的表格行。

So when page loads, if the radio button for the "yes" value is selected it should show the tables, if no then it should hide them. 因此,在页面加载时,如果选中“是”值的单选按钮,则应显示表,如果没有,则应将其隐藏。 IF no radio button is selected, it should also hide them. 如果未选择任何单选按钮,则也应将其隐藏。

Can someone help to add these additions? 有人可以帮助添加这些添加内容吗?

$('input[type=\"radio\"]').click(function(){
    if($(this).attr('value')=='yes'){
        $('.showCta').show();
    }
    if($(this).attr('value')=='no'){
        $('.showCta').hide();
    }
});

You can extract the main logic in an function like bellow, This is because you won't repeat your code. 您可以在下面的函数中提取主要逻辑,这是因为您不会重复代码。

function showTablesByRadio(){
    if($(this).attr('value')=='yes'){
        $('.showCta').show();
    }
    if($(this).attr('value')=='no'){
        $('.showCta').hide();
    }
}

call That function on click of radio & for each radio on load. 单击收音机并在加载的每个收音机上调用该函数。

$('input[type=\"radio\"]').click(showTablesByRadio);

$(document).ready(function(){
    $('input[type=\"radio\"]').each(showTablesByRadio);
})

Use .val() and change event. 使用.val()change事件。 Also you don't need to escape quotes like \\" 另外,您也不需要转义\\"引号

$('input[type="radio"]').change (function(){
    if($(this).val() =='yes'){
        $('.showCta').show();
    }
    if($(this).val() == 'no'){
        $('.showCta').hide();
    }
}).change(); //Trigger intially

This should be a pretty simple fix since all the current code you have should still work. 这应该是一个非常简单的修复程序,因为您拥有的所有当前代码仍然可以使用。 Try this: 尝试这个:

The following handles the on page ready: 以下内容可以处理页面上的内容:

$(document).ready( function() {
  var $x = $('input[type=\"radio\"]');

  if($x.attr('value')=='yes'){
            $('.showCta').show();
  }
  if($x.attr('value')=='no'){
            $('.showCta').hide();
  }
});

And keep the event handler as well: 并保留事件处理程序:

$('input[type=\"radio\"]').click(function(){
    if($(this).attr('value')=='yes'){
        $('.showCta').show();
    }
    if($(this).attr('value')=='no'){
        $('.showCta').hide();
    }
});

Well you can do this: 好吧,你可以这样做:

HTML: HTML:

<input id='yes' type='radio' name='check' checked>Yes</input>
<input id='no' type='radio' name='check'>No</input>
<button id='button'>Button</button>

JS: JS:

$(document).ready(function() {
    if (document.getElementById('yes').checked) {
        $('#button').show();
    } else {
        $('#button').hide();
    }
});

While the answers posted so far work, I'd still advise further changes to condense your code a little (you're using jQuery, the slogan for which is: "write less, do more." Take advantage of that): 虽然到目前为止发布的答案都行得通,但我仍然建议您进行进一步的更改以使您的代码稍微紧凑(您使用的是jQuery,其口号是:“写得更少,做得更多。”充分利用这一点):

// You don't need to escape the quotes (unless you delimit the string with
// the same quotes you use within your string).
// bind event-handling to the 'change' event:
$('input[type="radio"]').change(function(){
    // because this should only execute according the condition of the
    // checked 'input' element:
    if (this.checked){
        // show, or hide, the element(s) according to the assessment evaluating
        // to true (show) or false (hide):
        $('.showCta').toggle(this.value === 'yes');
    }
// trigger the change event, and thus the change-event handling, on page-load:
}).change();

JS Fiddle demo . JS小提琴演示

References: 参考文献:

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

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