javascript - 无法获取根据下一页中的信息更改颜色的按钮

标签 javascript html

我正在尝试根据 sessionStorage 项目是否标记为 true 来更改按钮的背景颜色。在下一页的“厨房”页面上,当选中所有按钮时,sessionStorage 项将变为 true,因此当您返回“主页”时,厨房现在标记为绿色而不是红色。至此演出圆满结束。我认为你可以使用 jQuery,但我真的不知道如何使用它。

首页

function loader(){
        var form = document.getElementById("myForm");

        //error somewhere in here
        for(var i=0;i<form.length;i++){
            if(sessionStorage.getItem(form.elements[i].value) == "true"){
                document.getElementById(form.elements[i].value).style.backgroundColor = "green";
                return true;
            }else{
                document.getElementById(form.elements[i].value).style.backgroundColor = "red";
                return false;
            }
        }
    }

    function initial(){
        if (localStorage.getItem("hasCodeRunBefore") === null) {
                var form = document.forms[0];
                var tot = document.forms[0].length;
                var i;
                for(var i = 0; i < tot ; ++i){
            document.getElementById(form.elements[i].value).style.backgroundColor = "red";
                }
            localStorage.setItem("hasCodeRunBefore", true);
        }
    }

    function start(){
        initial();
        loader();
    }

主页 HTML

<body onload="start()">
<header align=center>Home Screen</header>
<p align=center id="result"></p>
<script>
    document.getElementById("result").innerHTML = sessionStorage.getItem("currentAddress");
</script>

<ul align=center>
    <form id="myForm">
        <li>        
                <input type="button" name="area" id="livingroom" value="Living Room" ><br><br>
                <script type="text/javascript">
                        sessionStorage.setItem("livingroom","false");
                        document.getElementById("livingroom").onclick = function () {
                        location.href = "url";
                    };
                </script>
        </li>
        <li>        
                <input type="button" name="area" id="kitchen" value="Kitchen"><br><br>  
                <script type="text/javascript">
                        sessionStorage.setItem("kitchen","false");
                        document.getElementById("kitchen").onclick = function () {
                        location.href = "url";
                    };
                </script>
        </li>
        <li>
                <input type="button" name="area" id="bathroom" value="Bathroom" ><br><br>   
                <script type="text/javascript">
                        sessionStorage.setItem("bathroom","false");
                        document.getElementById("bathroom").onclick = function () {
                        location.href = "url";
                    };
                </script>
        </li>
        <li>        
                <input type="button" name="area" id="dining" value="Dining" ><br><br>   
                <script type="text/javascript">
                        sessionStorage.setItem("dining","false");
                        document.getElementById("dining").onclick = function () {
                        location.href = "url";
                    };
                </script>
        </li>
        <li>        
                <input type="button" name="area" id="bedroom1" value="Bedroom 1" ><br><br>
                <script type="text/javascript">
                        sessionStorage.setItem("bedroom1","false");
                        document.getElementById("bedroom1").onclick = function () {
                        location.href = "url";
                    };
                </script>       
        </li>
    </form>
</ul>

厨房

function checkTasks(form){
        var count = 0;
        for(var i=0;i<form.task.length;i++){
            if(form.task[i].checked){
                count++;
            }
        }

        if(count == form.task.length){
            sessionStorage.setItem("kitchen","true");
            window.open("url");
        }else{
            alert("You have not completed all the tasks! Please check all the boxes to indicate you have completed the tasks. If there is an issue, write it in the other box.");
        }
    }   

厨房 HTML

<body>
<header align=center>Kitchen To Do List</header>
<form name="todolist" >
    <div id="button">
        <label>
            <input type="checkbox" name="task" value="1" ><p>Microwave</p>
        </label>
    </div>
    <div id="button">
        <label>
            <input type="checkbox" name="task" value="1"><p>Coffee Maker</p>
        </label>
    </div>
    <div id="button">   
        <label>
            <input type="checkbox" name="task" value="1"><p>Oven</p>
        </label>
    </div>
    <div id="button">
        <label>
            <input type="checkbox" name="task" value="1"><p>Dishwasher</p>
        </label>
    </div>
    <div id="button">
        <label>
            <input type="checkbox" name="task" value="1"><p>Stove Top</p>
        </label>
    </div>
    <div id="button">
        <label>
            <input type="checkbox" name="task" value="1"><p>Other</p>
        </label>
    </div>

    <textarea id="other"></textarea><br><br>
    <p align=center>
        <input type="submit" name="box" id="box" value="Complete" onclick="checkTasks(this.form)">
    </p>
</form>

<div class="close">
    <form align=center action="url" target="_self">
        <input type="submit" name="back" value="Back">
    </form>
</div>

最佳答案

每次加载主页时,您都会使用这些内联脚本将值设置为 false。删除 home.html 中的 sessionStorage.setItem("livingroom","false"); 调用,看起来一切都会正常。

关于javascript - 无法获取根据下一页中的信息更改颜色的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56344358/

相关文章:

javascript - 编写一个 JavaScript 函数来反转数字

javascript - OpenLayers 3 在不向 map 添加图层的情况下获取矢量特征/属性

javascript - 在不同的选择框上更改选择框值

html - 当它位于 div 标签中的句子中间时如何转到下一行?

html - 如何仅使用 HTML + CSS 更改图像

java - 将数据从 select 标签发送到 servlet

javascript - react 错误index.js :1452 Warning: Each child in an array or iterator should have a unique "key" prop

JavaScript - 检查长度值

html - CSS:具有悬挂缩进和 overflow hidden 但不会将其切断的元素?

html - 居中绝对div;适用于 firefox 但不适用于 chrome