【问题标题】:passing parameter from multiple jquery mobile page从多个jquery移动页面传递参数
【发布时间】:2015-01-05 12:26:18
【问题描述】:

这是我几个星期以来一直在与之抗争的问题,我希望有人能提供帮助。我有两个要从中返回 json 数据的表,表 1 返回产品类别列表,表 2 返回产品列表。我已经设法将从类别表返回的 json 数据连接到我的查询移动应用程序中的列表视图我现在的挑战是当我单击类别时我似乎无法让我的列表视图显示产品信息 - 我正在使用多页应用程序使用 jquery mobile 请帮助

总结 - 我需要知道如何传递所选产品类别的 ID 以附加到我正在进行的 REST 调用以获取产品详细信息。

如果您需要更清晰的信息,请在此处发布。

【问题讨论】:

  • 你能显示一些代码吗?

标签: jquery json rest mobile


【解决方案1】:

首先我们创建页面

product.html

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
 </head>

<body>    
    <div data-role="page" id="product_page">
        <div data-role="header" class="">
             <h3>Home</h3>            
        </div>
        <div role="main" id="" class="ui-content">   
            <ul data-role="listview" id="product_list"></ul>  
        </div>
    </div>         
 </body>
 <script>

 var product = {
 "product": [
 {
  "product_name": "Product 1",
  "product_category": "1"
 },
 {
  "product_name": "Product 2",
  "product_category": "2"
 },
 {
  "product_name": "Product 3",
  "product_category": "3"
 }
 ]
 }

 $("#product_page").on("pageshow", function(event){
   var list = "";
   $.each(product, function(key, value){
      $.each(value, function(key, value){
        list += '<li><a href="category.html?product_category='+value.product_category+'" data-ajax="false">'+value.product_name+'</a></li>';
    })

    })    
     $("#product_list").html(list).trigger("create")
     $("#product_list").listview( "refresh" )
 })
</script>
<html>

category.html

<!DOCTYPE html>
<html>

    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js">  </script>
</head>

<body>    
    <div data-role="page" id="category_page">
       <div data-role="header" class=""></div>
       <div role="main" id="" class="ui-content">   
         <p id="detail"></p>
       </div>
    </div> 
</body>
<script>

$("#category_page").on("pageshow", function(event){
    var product_category = getParameterByName("product_category");
    $("#detail").html(product_category);    
})   
/** GET PARAMETER **/
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return results[1];
}
</script>
<html>

【讨论】:

  • 非常感谢韦德。这绝对给了我正在寻找的解决方案。另一方面。是否建议在 pageinit 调用中返回 1 个包含产品类别和产品的大 json 数据,然后按照上面显示的方式对其进行操作,或者我应该在每次点击 categoryitem 时进行 json 调用以过滤产品列表.谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 2020-04-21
  • 2016-01-24
  • 2011-11-19
相关资源
最近更新 更多