【问题标题】:onchange javascript to submit form breaks in jquerymobileonchange javascript 在 jquerymobile 中提交表单中断
【发布时间】:2013-05-24 20:04:31
【问题描述】:

我正在使用这样的 javascript...

<select onchange="document.langForm.submit();" ... >

但是当我开始使用 jquerymobile 时就坏了。

我知道这个问题已经出现过 100 次了。但是之前案例的超链接在这里没有帮助,因为我已经阅读了几十篇,不幸的是我不够聪明,无法理解任何其他讨论。严重地。我完全糊涂了。

另一方面,我已经抽出时间制作了一个非常非常简单的示例来展示我的头痛。所以,如果有人能准确地告诉我如何修复这两个页面,那将不胜感激。

http://activemetrics.ch/test/en.html

http://activemetrics.ch/test/de.html

请注意,您可以在浏览器中加载其中一个,然后通过更改选择列表中的选择切换到另一个页面。但是,在您到达第二页后,您无法使用类似的操作返回到第一页。

是的,这与 ajax 有关。我知道那么多。但是对我来说,在这两个页面上解决问题以允许用户轻松地在两个页面之间来回切换的最佳方法是什么?

欢迎所有建议。

非常感谢任何花费一些脑电波为我考虑这个问题的人!

-jd

【问题讨论】:

    标签: javascript ajax jquery-mobile form-submit onchange


    【解决方案1】:

    说明:

    要了解这种情况,您需要了解 jQuery Mobile 的工作原理。它使用 ajax 加载其他页面。

    首页正常加载。它的 HEADBODY 被加载到 DOM 中,它们在那里等待其他内容。加载第二个页面时,只有其 BODY 内容会加载到 DOM 中。

    根据我在您的回答中的内容,您可以理解这一点,但是您犯了一个错误。有 2 个 html 文件:en.htmlde.html,但它们的名称并不重要。重要的是 data-role="page" div 的页面 id 或 id 属性。在您的情况下,您有 2 个页面具有相同的 div 和相同的选择 ID。因为它们都被加载到 DOM 事件绑定中,所以总是会转到第一个加载的页面。

    您需要为您的页面和选择框使用唯一 ID。

    工作示例:

    zh.html

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>My Page</title> 
            <meta name="viewport" content="width=device-width, initial-scale=1"/> 
            <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
            <script                 src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
            <script                 src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
        </head> 
        <body>      
            <div data-role="page" id="sbePage1">
                <h3>this is english</h3>      
                <form name="langForm" id="langForm" action="BookNow.asp?" method="get" style="margin-top:0px auto">
                    <select data-inline="true" name="lang" id="langId" data-native-menu="false" >
                        <option value="1972" SELECTED>Something Already Selected</option>
                        <option value="1973">Some Sort of Change</option>
                    </select>
                </form> 
                <script>
                    $(document).on('pageinit', '#sbePage1' ,function () {   
                        $(document).off('change', '#langId').on('change', '#langId',function (event) {          
                            $.mobile.changePage('de.html', {
                                type: 'get',
                                data: ''
                            });
                        });
                    });   
                </script>           
            </div><!-- /page -->      
        </body>
    </html>
    

    de.html

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>My Page</title> 
            <meta name="viewport" content="width=device-width, initial-scale=1"/> 
            <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
            <script                 src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
            <script                 src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
        </head> 
        <body>       
            <div data-role="page" id="sbePage2">
                <h3>this is german</h3>      
                <form name="langForm2" id="langForm2" action="BookNow.asp?" method="get" style="margin-top:0px auto">
                    <select data-inline="true" name="lang" id="langId2" data-native-menu="false" >
                        <option value="1972" SELECTED>Something Already Selected</option>
                        <option value="1973">Something Else</option>
                    </select>
                </form> 
                <script>
                    $(document).on('pageinit', '#sbePage2' ,function () {
                        $(document).off('change', '#langId2').on('change', '#langId2',function (event) {            
                            $.mobile.changePage('en.html', {
                                type: 'get',
                                data: ''
                            });
                        });
                    });             
                </script>
            </div><!-- /page -->     
        </body>
    </html>
    

    编辑:

    虽然我理解你的愿望,但我仍然必须告诉你,这是不可能的。在当前示例中,您不能有 2 个与 jQuery Mobile 具有相同 id 的页面。想想看,您将有 2 个具有相同 id 的页面加载到 DOM 中。每次您尝试访问页面 DIV 时,您总是会访问第一页,因为它排在第一位。

    如果您想创建一个多语言页面,您不需要多个 html 页面。只需寻找一个好的 jQuery 多语言插件。

    另一方面,如果你关闭 ajax,你可以做你想做的事,但你会失去动画过渡。

    没有 ajax 但页面具有相同 id 的示例

    zh.html:

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>My Page</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
    
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
        <script                 src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script language="text/javascript">
              $(document).bind("mobileinit", function () {
                    $.mobile.ajaxEnabled = false;
              });
        </script>   
        <script                 src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
    </head> 
    <body> 
    
    <div data-role="page" id="sbePage1en">
    <h3>this is english</h3>      
                <form name="langFormEn" id="langFormEn" action="BookNow.asp?" method=get style="margin-top:0px auto">
                    <select data-inline="true" name="lang" id="langId" data-native-menu="false" >
                        <option value="1972" SELECTED>Something Already Selected</option>
                        <option value="1973">Some Sort of Change</option>
                    </select></form> 
                <script>
                    $("#sbePage1en").live('pageshow', function () {
                        $('#langFormEn').bind('change', function (event) {
                            $.mobile.changePage('de.html', {
                                type: 'get',
                                data: ''
                            });
                        });
                    });
                </script>               
    </div><!-- /page -->
    </body>
    </html>
    

    de.html

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>My Page</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
    
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
        <script                 src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script language="text/javascript">
              $(document).bind("mobileinit", function () {
                    $.mobile.ajaxEnabled = false;
              });
        </script>       
        <script                 src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
    </head> 
    <body> 
    
    <div data-role="page" id="sbePage1de">
    <h3>this is german</h3>      
                <form name="langFormDe" id="langFormDe" action="BookNow.asp?" method=get style="margin-top:0px auto">
                    <select data-inline="true" name="lang" id="langId" data-native-menu="false" >
                        <option value="1972" SELECTED>Something Already Selected</option>
                        <option value="1973">Something Else</option>
                    </select></form> 
    <script>
        $("#sbePage1de").live('pageshow', function () {
            $('#langFormDe').bind('change', function (event) {
                $.mobile.changePage('en.html', {
                    type: 'get',
                    data: ''
                });
            });
        });
    </script>               
    </div><!-- /page -->
    </body>
    </html>
    

    【讨论】:

    • 嗨 Gajotres,非常感谢您的意见。我尝试根据您的建议更新 html,它似乎和以前一样。这两个页面现在有单独的 id。还是我误解了你提出的改变?顺便说一句,我强烈希望在保持所有 ID 相同的同时完成这项工作,因为我们在这里讨论的是完全相同的内容,但使用了两种不同的风格/语言。仅仅因为文本将使用一种或另一种语言,就在各处制作不同的 ID 似乎很奇怪/不方便。
    • 我在我的答案中添加了新内容。看看吧。
    • 谢谢!我可以看到您的示例完全按照我的要求工作。我将研究此 html 以及您的 cmets,以尽可能地了解这些问题。谢谢!
    • 看看我的第二个例子(最后),这是你的代码,但关闭了 ajax。
    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多