如何更新localStorage变量并发送到Ajax调用

时间:2016-11-20 02:50:27

标签: javascript jquery ajax html5

我有一个选项列表。当您单击一个选项时,我想使用单击链接的data-id值更新localStorage值,然后通过Ajax调用发送更新的localStorage值,但问题是localStorage值实际上没有得到更新,除非窗口刷新。有没有办法在没有完整窗口刷新的情况下更新localStorage值:

HTML

<div class="phonechoice" data-id="1">Option 1</div>
<div class="phonechoice" data-id="2">Option 2</div>

JQUERY

 //Update localStorage variable on click and send to ajax call

 $(document).on("click", '.phonechoice', function(){
    var mcid=$(this).data("id");
    localStorage.mastercaseid = mcid; // CHANGE LOCALSTORAGE VALUE
    // location.reload(); //WANT TO STOP THIS RELOAD 
    getresult(url_pullrecords); //execute ajax call
  });

AJAX CALL

         function getresult(url_pullrecords) {
             $.ajax
             ({
                 context: this,
                 crossDomain: true,
                 url: url_pullrecords,
                 type: "GET",
                 data:  {mcid:localStorage.mastercaseid}, 
                 //THIS VALUE ABOVE DOESN'T GET UPDATED UNLESS WINDOW IS REFRESHED
                 beforeSend: function(){
                 },
                 success: function(data){
                   $(".btnholder").before(data);
                 }                   
             });
            }

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).on("click", '.phonechoice', function(e){
     var mcid=$(this).data("id");
     localStorage.setItem("mastercaseid", mcid); // CHANGE LOCALSTORAGE VALUE
      // location.reload(); //WANT TO STOP THIS RELOAD 
     e.stopPropagation();
     getresult(); //execute ajax call
 });