简体   繁体   English

用于单选按钮和显示/隐藏div的jQuery或Javascript解决方案

[英]jQuery or Javascript solution for radio buttons and showing/ hiding divs

I have, what on initial viewing, is a simple requirement to show/hide two divs based on a users response to radio buttons. 最初查看时,我有一个简单的要求,即根据用户对单选按钮的响应show/hide两个divs

See the JsFiddle: http://jsfiddle.net/P75cv/ 参见JsFiddle: http//jsfiddle.net/P75cv/

The real complexity is as follows. 实际的复杂性如下。

Div one is to be shown if any of the questions has a radio button value of 2 or 1. Div two is only to be shown if any of the questions don't have a value of 2, 1 or -1. DIV one要被显示,如果任何的问题,具有2或1格单选按钮值two 被显示,如果任何的问题,不具有的2,1的值或-1。

However, I also need a further check to do the following, if div one has been shown and a user goes back and changes their response so that no radio buttons have a value or 2 or 1, then div one needs to be hidden and div two shown. 但是,我还需要做进一步的检查以执行以下操作:如果已显示div one并且用户返回并更改其响应,以便没有单选按钮的值或为2或1,则需要隐藏one div,然后将div显示two Similarly, if div two is being shown but the user goes back and changes their responses so that any of the radio buttons have a value of 2 or 1, div two needs to be hidden and div one shown. 类似地,如果显示了div two ,但用户返回并更改其响应,则任何单选按钮的值为2或1,需要隐藏div two并显示div one

And finally, the user has the option of saving and coming back to the page - if they do, I need the right div to be shown based (on page load?). 最后,用户可以选择保存并返回页面-如果这样做,我需要根据显示的正确div(基于页面加载?)。

The simple part of showing and hiding divs I can do, in both jQuery and Javascript but this level of complexity is really causing me a headache - can anyone offer an insights or solutions? 在jQuery和Javascript中显示和隐藏div的简单部分,但是这种复杂性确实让我头疼-任何人都可以提供见解或解决方案吗?

I would create your basic functions that perform the show/hide routine. 我将创建执行show / hide例程的基本功能。 Then populate an array that has all the values of the radio buttons, check if the array contains the values you are looking for, and then call the proper function. 然后填充具有单选按钮的所有值的数组,检查该数组是否包含要查找的值,然后调用适当的函数。

I would use JQuery cookie to store what div should be shown when the user saves and comes back to the page. 我将使用JQuery cookie来存储当用户保存并返回页面时应显示的div。

So, answering my own question - I have used the following code: 因此,回答我自己的问题-我使用了以下代码:

<?php
function checking() {
count = 0;
";
for ($i=1; $i<=10; $i++){
echo"   q".$i." = $('input[name=\"q".$i."\"]:checked').val();
    if(q".$i." == 1 || q".$i." == 2){
            document.getElementById('disagree').style.display = 'block';
            document.getElementById('agree').style.display = 'none';
        count = 1;
    }
    ";

    }

    echo" 

    if (count==0){
        document.getElementById('disagree').style.display = 'none';
        document.getElementById('agree').style.display = 'block';
    }

}
?>

As you can see, I have 10 questions on the page and use PHP to write the script out and it works. 如您所见,我在页面上有10个问题,并使用PHP编写了脚本并成功运行。 I have no doubt whatsoever that there are far more efficient and elegant ways of achieving the same. 毫无疑问,我可以找到更有效,更优雅的方法来达到目的。

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

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