2017-05-23 105 views
0

我有这个代码,我需要将“totalcompra”id的值传递给另一个页面。 page1.html传递两个html之间的值

<div id="total"> 
    <div class="container-total"> 
     <div class="text-total"> 
       <p>Total:$</p><p id="totalcompra" name="totalcompra"></p> 
     </div> 
     <div class="ahr"> 
       <input type="submit" id="compra_segura" class="submit btn btn-success action-button" value="Compra Segura"/> 
     </div> 
    </div> 
</div> 

我想把这里的价值。

page2.html

<aside> 
    <div id="side-container"> 
      <h2 class="quoter-text">Resumen de compra</h2> 
      <div class="rsm"></div> 
      <div class="pymt-total"><p class="total-pym">Total:</p></div> 
    </div> 
</aside> 

AJAX

$('#compra_segura').click(function() { 
    $.ajax({ 
     type: 'POST', 
     url: '/payment/', 
     data: { total : $('#totalcompra').text() }, 
     success: function(data) 
     { 
      $('.total-pym').html(data); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert(xhr.status); 
      alert(thrownError); 
     } 
    }); 
}); 

我试着用Ajax和jQuery这样做,但我一直没成功。 我该怎么办?谢谢。

+1

分享你的ajax/jquery cod即 – tech2017

+0

你需要阅读[.load()](http://api.jquery.com/load/)和[示例](http://www.jquery-tutorial.net/ajax/the-load-method /) –

+0

也使按钮成为'type =“按钮'' – mplungjan

回答

1

您可以使用localStoragedocs

$('#compra_segura').click(function() { 
    $.ajax({ 
     type: 'POST', 
     url: '/payment/', 
     data: { total : $('#totalcompra').text() }, 
     success: function(data) 
     { 
      localStorage.setItem("totalPayment", data); //setter 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert(xhr.status); 
      alert(thrownError); 
     } 
    }); 
}); 

并使用var x = localStorage.totalPayment;获得的价值page2.html

+0

谢谢,它对我来说非常好。 –

0

首先,看你的Ajax调用的返回,如果data变量返回一个JSON,然后你必须以这种方式访问​​购买的价值:

$('.total-pym').append(data.totalcompra); 
+2

表明“数据未定义” –