简体   繁体   English

重新载入页面时,Javascript更改重置

[英]Javascript changes reset when page reloads

I have a one page checkout for my site, so i decided to separate it to 5 parts, and make one or two display at a time, using javascript. 我的网站有一页结帐,所以我决定将其分为5部分,并使用javascript一次显示一两个。

see the code below: 参见下面的代码:

    <fieldset id="vm-fieldset-pricelist" class="vm-fieldset-pricelist"> ... </fieldset>

    <div id="coupon-section" class="coupon-section display-none"> ... </div>

    <fieldset id="shipment-section" class="shipment-section display-none"> ... </fieldset>

    <div id="edit-account-section" class="billto-shipto display-none"> ... </div>

    <div id="part-five" class="display-none"> ... </div>


    <?php
                    $user = JFactory::getUser();
                    if ($user->guest) { 
                        $usercheckin = '_notloggedin';
                    } else {
                        $usercheckin = '_loggedin';
                    } 
            ?> 

   <button type="button" id="one-to-two<?php echo $usercheckin ?>" class="sabz-button pull-right" onclick="cartButtonHandler(this.id); return false;"><?php echo vmText::_('COM_VIRTUEMART_CHECKOUT_TITLE')?> </button>

<script>

window.cartButtonHandler = function(button_id) {
    switch(button_id) {
    case 'one-to-two_notloggedin':
        document.getElementById("vm-fieldset-pricelist").className = "display-none";
        document.getElementById("com-form-login").className = "";
        document.getElementById(button_id).innerHTML = "ثبت اطلاعات و ادامه خرید";
        document.getElementById(button_id).className = "abi-button pull-right";
        document.getElementById(button_id).id = 'two-to-three';
        break;
    case 'one-to-two_loggedin':
        document.getElementById("vm-fieldset-pricelist").className = "display-none";
        document.getElementById("edit-account-section").className = "billto-shipto display-none";
        document.getElementById("shipment-section").className = "shipment-section";
        document.getElementById(button_id).innerHTML = "ثبت اطلاعات و ادامه خرید";
        document.getElementById(button_id).className = "abi-button pull-right";
        document.getElementById(button_id).id = 'two-to-three';
        break;
    case 'two-to-three':
        document.getElementById("edit-account-section").className = "display-none";
        document.getElementById("shipment-section").className = "display-none";
        document.getElementById("coupon-section").className = "coupon-section";
        document.getElementById(button_id).innerHTML = "بازبینی و تایید سفارش";
        document.getElementById(button_id).id = 'three-to-four';
        break;
   case 'three-to-four':
        document.getElementById("part-chahar").className = "";
        document.getElementById("vm-fieldset-pricelist").className = "vm-fieldset-pricelist";
        document.getElementById("edit-account-section").className = "billto-shipto display-none";
        document.getElementById("shipment-section").className = "shipment-section";
        document.getElementById("coupon-section").className = "coupon-section";
        document.getElementById("handlerbutton-section").className = "display-none";
        document.getElementById(button_id).id = 'four-to-five';
        break;
   case 'four-to-five':
        alert(button_id);
        break;
}
}

    </script>

First of all, I don't know javascript, is there a better way to do this in javascript or jquery or ...? 首先,我不了解javascript,是否有更好的方法在javascript或jquery或...中做到这一点?

My problem: when a user for example in "coupon-section" inputs a coupon code and clicks on the submit button, mind that coupon submit button is different from NEXT STEP button, the page reloads and first step pops up. 我的问题:例如,“优惠券部分”中的用户输入优惠券代码并单击“提交”按钮时,请注意,优惠券提交按钮与“下一步”按钮不同,页面会重新加载,并弹出第一步。 or when a user checks a check box, or any other interaction, page reloads and first step pops up. 或当用户选中复选框或任何其他交互时,页面会重新加载,并弹出第一步。

How can i save javascript changes and get them back on page reload so user can stay on the same step after interacting with the form? 我如何保存javascript更改并使它们重新加载到页面上,以便用户与表单进行交互后可以停留在同一步骤上? i guess it might be solved using cookies or something but i don't know how to do that. 我想可以使用Cookie或其他方法解决此问题,但我不知道该怎么做。

1.Go through Jquery for easy html (DOM) manipulations and selections. 1.通过Jquery轻松进行html(DOM)操纵和选择。

  1. Go through the documentation SessionStorage ,(to set and get items in broswer session on reload) 浏览文档SessionStorage,(在重新加载时在broswer会话中设置和获取项目)

  2. Learn Json , Create a json structure to record the progress , and make a function to load the section based on that json .(make everything driven by data) eg : 学习Json,创建一个json结构来记录进度,并创建一个函数基于该json加载该部分。(使所有内容均由数据驱动),例如:

    [ [

    { sectionName:"section1", progress:"started" }, { sectionName:"section2", progress:"incomplete" }, {sectionName:“ section1”,进度:“ started”},{sectionName:“ section2”,进度:“ incomplete”},
    ] ]

Hope this helps 希望这可以帮助

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

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