【问题标题】:How can I return user to a specific div working with current code如何将用户返回到使用当前代码的特定 div
【发布时间】:2013-12-26 22:43:11
【问题描述】:

我有一份包含 12 个部门的表单问卷。第一个div 是显示块,其他的是无显示。然后在单击时使用选项卡功能显示和隐藏 div。最后一个 div 是对由 ajax 调用加载的表单的回顾。下面的代码运行良好,直到我想添加一个按钮,以便用户可以返回并回答未回答的问题

这段代码运行良好** $(文档).ready(函数 () { var tab_pool = [“Q1”、“Q2”、“Q3”、“Q4”、“Q5”、“Q6”、“Q7”、“Q8”、“Q9”、“Q10”、“Q11”、“Q12” "]; var visible = $(".tab:visible").attr('class').split(" ")[1]; var curr_ind = $.inArray(visible, tab_pool);

        $('.next').click(function () {
             $("#processing").show();
                $.ajax({
                    type: "POST",
                    url: "process.php",
                    data: $('.data').serialize() + "&data=" + data,
                    success: function (data, status, xhr) {
                        if (xhr.getResponseHeader("DB_SUCCESS") == 1) {
                 $("#processing").hide();
                        } else {
                 $("#processing").hide();
                            alert("Save failed");
                return false;
                        }
                    }
                });
    // There are conditions here such as if this then
    //  curr_ind = curr_ind + whatever to move to next relevant question 
        if (curr_ind < 11) {
            $(".tab:visible").delay(750).hide(0);
         $("#processing").delay(750).hide(0);
            curr_ind = curr_ind + 1;
            $("." + tab_pool[curr_ind]).delay(750).show(0);
            $(".finished").delay(750).hide(0);
            $(".back").delay(750).show(0);
        }
    // if the user has reached the end then below we show the review of questionaire
        if (curr_ind === 11) {
         $("#processing").delay(750).hide(0);
            $(".finished").delay(750).show(0);
            $.ajax({ 
              type: "POST",
              url: 'review.php',
            data:  "username=" + username,
            success: function (data, status, xhr) {
             if (xhr.getResponseHeader("DB_SUCCESS") == 1) {
             $("#review").show();
                $('#review').html(data);
              } else {
                alert("Review not available");
                return false;
                }
            }
            });
             }
        });
    // after the first question a back button is display for
    //  user to go back to previous question
        $('.back').click(function () {
            if (curr_ind > 0) {
                $(".tab:visible").hide();
    // There ar conditions here so user is brought back to 
    // the right div and not one that was skipped from conditions above     
                $("." + tab_pool[curr_ind]).show();
        document.getElementById(tab_pool[curr_ind]).checked=false;
                $(".finished").hide();
                $(".next").show();
            }
            if (curr_ind === 0) {
                $(".back").hide();
            }
        });

结束工作代码**

// **** what I want here is a button for unanswered questions that will bring user
// back to that specific unaswered question
// I tried assigning a class and a numeric value to those buttons but
// for some reason I get a new blank page??? 
// I tried a few variations of the code below 
        function answerQ (value) {
        var returnTo = value;
            curr_ind = curr_ind - returnTo;
                $(".tab:visible").hide();
                $("." + tab_pool[curr_ind]).show();
        document.getElementById(tab_pool[curr_ind]).checked=false;
                $(".finished").hide();
                $(".back").hide();
                $(".next").show();
        }
    });

** 返回未回答问题的按钮示例

回答

【问题讨论】:

  • 可能使用cookie来跟踪用户的访问,然后使用它来决定显示哪些div

标签: javascript php ajax


【解决方案1】:

最简单的可能是设置一个cookie。

另一种选择是设置 localStorage 或 sessionStorage(如果您可以指望它们拥有现代浏览器)。

有关该主题的一些阅读:
http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-learning-about-html5-local-storage/ http://www.sitepoint.com/html5-web-storage/
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

【讨论】:

  • 至少添加对localStorage api的引用。
  • @Jamis Charles 我会看看你的建议
  • @CornHoleLI 如果有帮助,请随意接受其中一个答案。
  • 现在我想我应该把这个作为一个新问题来问
【解决方案2】:

您需要记住他们查看表单的次数。您没有提到 PHP,但您将其作为标签,所以我假设您正在使用它。您可以创建一个会话变量来存储用户查看页面/表单的次数。 (这个伪代码是在深夜写的,没有经过测试,但你应该能够得到它的要点)

if (!isset($_SESSION['viewCnt']))
    $_SESSION['viewCnt'] = 1;
else { //not the first time viewing the page/form, so increment counter and setup ajax
    $_SESSION['viewCnt'] = $_SESSION['viewCnt']+1;
    echo'
    <script>
        $(function() {
            //do your ajax call here to fill div6
        });
    </script>
    ';
}

echo'
<div id="1" style="display:'.($_SESSION["viewCnt"] == 1 ? 'block' : 'none').';"></div>
<div id="2" style="display:none;"></div>
<div id="3" style="display:none;"></div>
<div id="4" style="display:none;"></div>
<div id="5" style="display:none;"></div>
<div id="6" style="display:'.($_SESSION["viewCnt"] == 1 ? 'none' : 'block').';"></div>
';

如果您不想使用会话变量,则可以使用 cookie 或 HTML5 网络存储:http://www.w3schools.com/html/html5_webstorage.asp

注意:会话方法将在每次新登录应用程序时重新开始,而 cookie 方法将根据过期日期或用户清除临时 Internet 文件的时间重新开始

【讨论】:

  • 最好在答案中添加注释。
  • @gfrobenius 感谢您的快速回复。划分相当长,这有点不切实际。另外,ajax 会在 php 中运行吗?
  • 是的,如果你的 div 很大并且你不想要很大的 if 语句,那么还有其他方法,我会编辑我的回复。
猜你喜欢
  • 2017-10-16
  • 2017-04-20
  • 2014-12-04
  • 2019-06-12
  • 1970-01-01
  • 2013-03-24
  • 2015-11-27
  • 1970-01-01
  • 2018-03-10
相关资源
最近更新 更多