简体   繁体   English

如果检查了某些单选按钮,显示div则显示不同的div?

[英]If certain radio buttons checked, display div else display different div?

:) So I'm trying to create a user questionaire type form, it doesnt have to send to anyone just display a div once certain radio buttons are checked and if them certain radio buttons arent checked then display a different div, but also when the webpage opens/refreshes none of the div's I want to display to be displayed (if that makes sense?). :)所以我正在尝试创建一个用户问号类型的表单,它不必发送给任何人只是显示一个div一旦确定某些单选按钮,如果他们某些单选按钮没有检查然后显示一个不同的div,但也网页打开/刷新我想要显示的div没有显示(如果这有意义吗?)。 I found some code on here to try but it didnt work for me, I'm a noob with JS & Jquery but was hoping anyone could shed some light on my problem. 我在这里找到了一些代码尝试,但它对我没有用,我是JS和Jquery的菜鸟,但我希望有人能解释我的问题。 please find my code below. 请在下面找到我的代码。

Jquery? jQuery的?

if($('input[value=yes1]:checked,
 input[value=yes2]:checked, 
 input[value=yes3]:checked,
 input[value=yes4]:checked').length == 4){

$("#correct").show();

 }else{

$("#correct").hide();

HTML HTML

<div class="row">
<div class="left">

    <div class="right answer">
        <div class="leftradio">
            <input type="radio" id="yes1" value="yes1" name="iCheck1">
            <label>Yes</label>
            </input>
        </div>

        <div class="rightradio">
            <input type="radio" id="no1" name="iCheck1">
            <label>No</label>
            </input>
        </div>

    </div>

</div>
<div class="right">

    <div class="right answer">
        <div class="leftradio">
            <input type="radio" id="yes2" value="yes2" name="iCheck2">
            <label>Yes</label>
            </input>
        </div>

        <div class="rightradio">
            <input type="radio" id="no2" name="iCheck2">
            <label>No</label>
            </input>
        </div>

    </div>
</div>

Yes No Yes No 是否是否

CSS CSS

 #correct{width:100%; height:50px; background:green; display:none;} #incorrect{width:100%; height:50px; background:red; display:none;} 

I think this is what you're looking for. 我想这就是你要找的东西。 I had to change the number of "correct" answers to two because that's all the html you have. 我不得不将“正确”答案的数量改为2,因为这就是你所拥有的所有html。

JS: JS:

$( "input" ).on( "click", function() {
    if($('input[value=yes1]:checked, input[value=yes2]:checked').length === 2){
       $("#correct").show();
    }else{
       $("#correct").hide();
    }
});

Working JS Fiddle: http://jsfiddle.net/akj84vrq/12/ 工作JS小提琴: http//jsfiddle.net/akj84vrq/12/

A little something like this would probably get you what you want. 这样的小东西可能会让你得到你想要的东西。 http://jsfiddle.net/uu512erm/ http://jsfiddle.net/uu512erm/

Listen for change event on the radio inputs by $('input:radio').change(function(){} and then just add the logic inside. 通过$('input:radio').change(function(){}监听无线电输入上的变化事件$('input:radio').change(function(){}然后只需在里面添加逻辑。

I'm not sure that this can be accomplished via CSS. 我不确定这可以通过CSS完成。

Now, you are checking against some values/elements that don't exist in your code (ie input[value=yes3]:checked , you don't have input with that value, check it for yourself). 现在,您正在检查代码中不存在的某些值/元素(即input[value=yes3]:checked ,您没有输入该值,请自行检查)。

I don't understand what exactly you want to accomplish, but I will still try to help and since you're new let's keep it very basic: 我不明白你想要完成什么,但我仍然会尝试提供帮助,因为你是新手让我们保持基本:

if ($('input[value=yes1]').is(':checked') && $('input[value=yes2]').is(':checked')) {
  // this will fire only if input with values yes1 and yes2 is checked
  $('div_to_show').show(); // put your own selectors here
  $('div_to_hide').hide(); // put your own selectors here
} 

if ($('input[value=no1]').is(':checked') && $('input[value=no2]').is(':checked')) {
  // this will fire only if input with values no1 and no2 is checked
  $('some_other_div_to_show').show(); // put your own selectors here
  $('some_other_div_to_hide').hide(); // put your own selectors here
}

if ($('input[value=yes1]').is(':checked') && $('input[value=no1]').is(':checked')) {
  # this will fire only if both input with values yes1 and no1 is checked
  $('some_other_div_to_show').show(); # put your own selectors here
  $('some_other_div_to_hide').hide(); # put your own selectors here
} 

# and so on... just type in your conditions and that's it. 

If you need some other cases, just add them using this pattern or construct one complex else if statement with all your scenarios. 如果您还需要其他一些案例,只需使用此模式添加它们,或者在所有场景中构造一个复杂的if语句。

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

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