简体   繁体   English

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

[英]How to update localStorage variable and send to Ajax call

I have a list of options. 我有一个选项列表。 When you click one option I want to update the localStorage value with the data-id value of the clicked on link and then send the updated localStorage value through an Ajax call, but the problem is that localStorage value doesn't get actually updated unless the window refreshes. 当您单击一个选项时,我想使用单击的链接的data-id值更新localStorage值,然后通过Ajax调用发送更新的localStorage值,但是问题是,除非窗口刷新。 Is there a way to have the localStorage value updated without a full window refresh: 有没有一种方法可以在不刷新整个窗口的情况下更新localStorage值:

HTML HTML

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

JQUERY 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 AJAX通话

         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);
                 }                   
             });
            }

Try this: 尝试这个:

$(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
 });

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

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