weixin_33698823 2011-01-21 07:28 采纳率: 0%
浏览 20

在AJAX表单中使用“返回”按钮?

Hi,

I have a ASP.NET MVC page that contains 5 cascading dropdowns(hidden until loaded). When the first is set AJAX will fetch data for the second dropdown (dynamic html).

If we navigates to the next page and then hit the "back" button in the browser only the first cascading dropdown will be shown and set even when we did set all 5?

Note : By setting a correct querystring the service is able to set the form just as if the Ajax have been used, but the service is never consulted with the back button.

Pleas Advice

  • 写回答

1条回答 默认 最新

  • weixin_33671935 2016-07-06 16:19
    关注

    What you need is HTML 5 history API. You can read about this in many places, for example here.

    Here's a simple example, HTML only (your whole problem is not ASP.NET MVC related but HTML related, this can happen on any platform):

    Let's say you have page 1 (index.html) :

    <!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
        <meta charset="utf-8" />
    </head>
    <body>
        <input type="text" id="txt1" />
        <br />
        <input type="text" id="txt2" />
        <br />
        <br />
        <button onclick="doStuff();">do stuff</button>
        <button onclick="goFurther();">go further</button>
    
        <script type="text/javascript">
    
            var qs = (function (a) {
                if (a == "") return {};
                var b = {};
                for (var i = 0; i < a.length; ++i) {
                    var p = a[i].split('=', 2);
                    if (p.length == 1)
                        b[p[0]] = "";
                    else
                        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
                }
                return b;
            })(window.location.search.substr(1).split('&'));
    
            window.onload = function () {
                var data1 = qs["txt1"];
                var data2 = qs["txt2"];
                if (data1 == undefined)
                    data1 = "";
                if (data2 == undefined)
                    data2 = "";
                var textbox1 = document.getElementById("txt1");
                var textbox2 = document.getElementById("txt2");
                textbox1.value = data1;
                textbox2.value = data2;
            }
    
            function doStuff() {
                var textbox1 = document.getElementById("txt1");
                var textbox2 = document.getElementById("txt2");
                history.pushState('abc', 'title', 'index.html?txt1=' + textbox1.value + "&txt2=" + textbox2.value);
            }
    
            function goFurther() {
                window.location = "/next.html";
            }
        </script>
    </body>
    </html>
    

    As you can see (and try in a project) the first button takes the data in two fields (the textboxes in my overly simplistic example) and sets the current URL to one with a querystring containing this data (again, being an overly simplified example it does not do validation, escaping, encoding etc.).

    The second button takes you to another page:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Next crp</title>
        <meta charset="utf-8" />
    </head>
    <body>
        asdf
    </body>
    </html>
    

    Which is put here only to be able to navigate to it. Navigating back to the first page (by pressing the back button in the browser) will lead you to the URL that was set via javascript with the querystring.

    The onload handler will repopulate elements with the data in the querystring.

    Don't forget to search online resources about 'HTML5 History API'.

    I hope this example will help.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况