简体   繁体   English

jQuery Mobile JS仅在刷新页面时有效

[英]jQuery Mobile js only works when I refresh page

I'm new to Jquery Mobile and I've tried everything to solve this issue. 我是Jquery Mobile的新手,并且已经尝试了所有方法来解决此问题。 I have the first page which is index.php, this page loads buttons from a js that gets the information from json. 我有第一页是index.php,此页面从js加载按钮,该js从json获取信息。 The second page (FichaTecnica.php) shows the information of that wine (everithing is in spanish, sorry). 第二页(FichaTecnica.php)显示了该葡萄酒的信息(抱歉,所有信息均为西班牙语)。 The problem is that I have to refresh the page in the browser to load the information and if I go back to the first page I also have to refresh to load the buttons. 问题是我必须刷新浏览器中的页面才能加载信息,如果返回第一页,我也必须刷新以加载按钮。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks. 谢谢。

Index.php Index.php

<div data-role="page" id="main">      
    <div data-role="header">         
        <h1>
            Page 1
        </h1>     
    </div>     

    <div data-role="content">
        <div>
            <img src="images/Vinos separador [Negro].png" />
        </div>
        <div data-role="controlgroup" data-role="controlgroup" id="buttonGroup">
        </div>


        <div>
            <img src="images/P Venta Separador [Negro].png" />
        </div>
        <div data-role="controlgroup" data-role="controlgroup" id="buttonGrouploc">
        </div>



    </div>     

    <div data-role="footer">              
    </div> 
</div>             

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script src="js/variedadeslist.js"></script>

Js File that loads the buttons in first page 载入第一页按钮的Js文件

        var url="http://localhost/CavaOnline/json_variedades.php";      
        var buttonHtmlString = "", pageHtmlString = "";

        var jsonResults;

    $.getJSON(url,function(data){
        jsonResults = data.items;
        displayResults();       
    });


    function displayResults() {

        for (i = 0; i < jsonResults.length; i++) {
            buttonHtmlString += '<a data-transition="slide" href="FichaTecnica.php?id=' + jsonResults[i].id + '" id="'+ jsonResults[i].id +'" data-role="button">' + jsonResults[i].nombre + '</a>';
        }

        $("#buttonGroup").append(buttonHtmlString);
        $("#buttonGroup a").button();   
    }

Second Page "FichaTecnica.php" 第二页“ FichaTecnica.php”

    <div data-role="page" id="pagina2">
        <div data-role="header">
            <h1>
                Header
            </h1>
        </div>

        <div data-role="content">

            <div>

                 <div data-role="collapsible" data-collapsed="false">
                <h1>El Vino</h1>
                <div id="descripcion">

                </div>
                </div>

                <div data-role="collapsible" data-collapsed="false">
                <h1>Cata</h1>
                <div id="cata">

                </div>
                </div>

            </div>

            <a data-role="button" id="botonMarcas"></a>

        </div>

        <div data-role="footer">
            <h1>
                footer
            </h1>
        </div>            
    </div>

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script src="js/descripcionlist.js"></script>  

Js that loads the information in the second page js在第二页中加载信息

$('#pagina2').live('pageinit',function(event){

var id =  getUrlVars()["id"];


$.getJSON('http://localhost/CavaOnline/json_variedades.php?id='+id, function(variedades) {


$.each(variedades, function(index, variedad) {

    $('#descripcion').append('<p>'+variedad[id - 1].descripcion+'</p>');
    $('#cata').append('<p>'+variedad[id - 1].cata+'</p>');
    $('#botonMarcas').append().attr("href", 'FichaTecnica.php?id=' + variedad[id - 1].id);


});

});

function getUrlVars() {

var vars = [], hash;

var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for(var i = 0; i < hashes.length; i++)

{

    hash = hashes[i].split('=');

    vars.push(hash[0]);

    vars[hash[0]] = hash[1];

}

return vars;

} }

It would be better to have your pageinit code wired up before you bring in the page. 最好在插入页面之前先将pageinit代码连接起来。 Therefore, make sure you include the JS as part of your first page. 因此,请确保将JS包含在首页中。

Wire up the paginit, like so: 连接paginit,如下所示:

$(document).on('pageinit', '#pagina2', function(e) {
    /* TODO: Page 2 init code */
});

Your initial page is not loading correctly because you're building the page incorrectly (by including the JS at the bottom after the other elements load). 您的初始页面未正确加载,因为您构建的页面不正确(在加载其他元素之后在底部添加了JS)。 Therefore, the JavaScript only takes effect after the page is refreshed since at that point the JS already exists in the DOM. 因此,JavaScript仅在刷新页面后才生效,因为此时JS已存在于DOM中。

Here's the basic page markup: 这是基本的页面标记:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Page Title</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-rc.2/jquery.mobile-1.2.0-rc.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0-rc.2/jquery.mobile-1.2.0-rc.2.min.js"></script>
</head> 

<body> 
    ...content goes here...
</body>
</html>

Also, if you're including any custom JS that will alter the page (like your pageint) they should be loaded AFTER jQuery is loaded, but BEFORE jQuery Mobile is loaded (so insert that JS file between the two in the head). 另外,如果您要包含将更改页面的任何自定义JS(如pageint),则应在加载jQuery之后加载,而在jQuery Mobile加载之前加载它们(因此,请在头部的两个之间插入该JS文件)。

I've had similar problems and just fixed them by adding data-ajax=false in the link. 我遇到过类似的问题,只是通过在链接中添加data-ajax = false来解决它们。

ref: http://api.jquerymobile.com/pagecontainer/ 参考: http : //api.jquerymobile.com/pagecontainer/

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

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