【问题标题】:Parameters are passing but not retrieving when mobile.changepage in jquerymobilejquerymobile中的mobile.changepage时参数正在传递但未检索
【发布时间】:2013-09-04 06:32:07
【问题描述】:

您好,当我单击搜索按钮时,我有一个带有搜索按钮和两个下拉列表值的搜索字段,我想使用 mobile.change 方法传递参数值。我正在使用如下结构

page1.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Client View</title>
<link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript">
  $(document).on('pagebeforeshow',function(){

    $("#searchbutton").on('click',function(){

   var searchvalue = $("[name='clientsearch']").val();
  var searchbyvalue = $("#select-choice-searchby").val();
  var statusvalue = $("#select-choice-status").val();

   $.mobile.changePage(
     'accountlist.html',
    { 
     //dataUrl : "accountlist.html?searchvalue="+searchvalue&"searchbyvalue="+searchbyvalue&"statusvalue="+statusvalue , 
    data : { 'searchvalue' : searchvalue,'searchbyvalue':searchbyvalue,'statusvalue':statusvalue }, 
    reloadPage : false, 
    changeHash : true 
    });
  });
});
 </script>
</head>

<body>
 <!--Client Page-->

         <div data-role="page" id="client" data-add-back-btn="true" >
         <div data-role="header" data-theme="b" data-position="fixed" data-id="persistantFooter">
 <h1>Client View</h1>
</div>
<div data-role="content">
    <div data-role="fieldcontain" id="searchbox">
     <input type="search" name="clientsearch" value="" data-inline="true" data-mini="true"/>
     <input type="button" value="Search" data-inline="true" data-theme="b" data-mini="true" id="searchbutton"/>
    </div>

    <div data-role="fieldcontain"  >
         <label for="select-choice-searchby"  class="select" data-inline="true" style = "width: 90px">Search By:</label>
         <select name="select-choice-searchby" id="select-choice-searchby" data-inline="true" data-mini="true" style = "float: right">
            <option value="account">Account</option>
            <option value="customhouseholds">Custom Households </option>
            <option value="roahouseholds">ROA Households</option>
        </select>
        </div>
        <div data-role="fieldcontain"  >
        <label for="select-choice-status"  class="select" data-inline="true" style = "width: 90px">Status:</label>
         <select name="select-choice-status" id="select-choice-status" data-inline="true" data-mini="true" style = "float: right">
            <option value="open">Open</option>
            <option value="closed">Closed </option>
            <option value="all">All</option>
        </select>
    </div>
</div>

</div>

 </body>
 </html>

page2.html:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Account view</title>
<link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript">
 $(document).on('pagebeforeshow',function(){
alert("hi");
//  var parameters = window.location.pathname.split("?")[1];
//alert(getUrlVars())
var searchvalue = getUrlVars()[0];
 var searchbyvalue = getUrlVars()[1];
 var statusvalue = getUrlVars()[2];
 alert(searchvalue);

function getUrlVars()
{
    var qName = [], hash;
    var qVal=[];
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        qName.push(hash[0]);
        qVal.push(hash[1]);
        qName[hash[0]] = hash[1];
    }
    return qVal;
    }
       });
     // JavaScript Document
    </script>
 </head>

  <body>
    <div data-role="page" id="accountlist" data-add-back-btn="true" >
    <div data-role="header" data-theme="b">
        <h3>Account View</h3>
    </div>
    <div data-role="content">

    </div> <!--content-->
</div><!--page-->
    </body>
        </html>

在page2.html中我们无法访问参数值,脚本函数也无法执行如何解决这个问题

【问题讨论】:

    标签: javascript jquery html jquery-mobile parameters


    【解决方案1】:

    试试这个:我考虑过单页概念,你可以相应地改变你的代码:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Client View</title>
    
      <link rel="stylesheet" href="http://view.jquerymobile.com/1.3.2/dist/demos/css/themes/default/jquery.mobile.min.css" />
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
      <script type="text/javascript" src="http://view.jquerymobile.com/1.3.2/dist/demos/js/jquery.mobile.min.js"></script>
    
    </head>
    
    <body>
     <!--Client Page-->
    
             <div data-role="page" id="client" data-add-back-btn="true" >
             <div data-role="header" data-theme="b" data-position="fixed" data-id="persistantFooter">
     <h1>Client View</h1>
    </div>
    <div data-role="content">
        <div data-role="fieldcontain" id="searchbox">
         <input type="search" name="clientsearch" value="" data-inline="true" data-mini="true"/>
         <input type="button" value="Search" data-inline="true" data-theme="b" data-param1="" data-param2 ="" data-param3="" data-mini="true"  id="searchbutton"/>
        </div>
    
        <div data-role="fieldcontain"  >
             <label for="select-choice-searchby"  class="select" data-inline="true" style = "width: 90px">Search By:</label>
             <select name="select-choice-searchby" id="select-choice-searchby" data-inline="true" data-mini="true" style = "float: right">
                <option value="account">Account</option>
                <option value="customhouseholds">Custom Households </option>
                <option value="roahouseholds">ROA Households</option>
            </select>
            </div>
            <div data-role="fieldcontain"  >
            <label for="select-choice-status"  class="select" data-inline="true" style = "width: 90px">Status:</label>
             <select name="select-choice-status" id="select-choice-status" data-inline="true" data-mini="true" style = "float: right">
                <option value="open">Open</option>
                <option value="closed">Closed </option>
                <option value="all">All</option>
            </select>
        </div>
    </div>
    
    </div>
    
    <div data-role="page" id="accountlist" data-add-back-btn="true" >
        <div data-role="header" data-theme="b">
            <h3>Account View</h3>
        </div>
        <div data-role="content">
    
        </div> <!--content-->
    </div><!--page-->
    
    <script type="text/javascript">
      $("#client").on('pageshow',function(){
    
        $("#searchbutton").on('click',function(){
    
       var searchvalue = $("[name='clientsearch']").val();
      var searchbyvalue = $("#select-choice-searchby").val();
      var statusvalue = $("#select-choice-status").val();
    
    
      $("#searchbutton").data("param1", searchvalue);
      $("#searchbutton").data("param2", searchbyvalue);
      $("#searchbutton").data("param3", statusvalue);
    
    
       $.mobile.changePage(
         '#accountlist', {     
        reloadPage : false, 
        changeHash : true 
        });
      });
    });
    
    $("#accountlist").on('pageshow',function(){
        alert( $("#searchbutton").data("param1"));
        alert( $("#searchbutton").data("param2"));
        alert( $("#searchbutton").data("param3"));
    });
    
     </script>
    
     </body>
     </html>
    

    【讨论】:

    • 我想在我的应用程序中实现 SEO(搜索引擎优化)概念。所以我只做多页概念
    • 如果上面的数据参数概念对你不起作用,他们不妨使用 wndow.sessionStorage 并设置和获取下一页的项目。
    • 在我上面的脚本代码中只在主体内部工作,但是当我将代码放入 head 标签时它不起作用..
    • 在 jquery mobile 中,你永远不应该将 pageshow 事件放在 head 部分,它应该总是放在页面 div 下方的 body 中,或者在加载所有脚本后放在另一个 js 文件中。
    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2014-03-14
    相关资源
    最近更新 更多