JS实现座位选择的问题

JavaScript022

JS实现座位选择的问题,第1张

代码如下。js+html。

但是如同上述 黑马黄子所说。需要考虑数据实时性。自己判断何时需要提交数据。

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

    <style type="text/css">

        .main{

            width: 99%

            height:99%

            border: 1px solid #dddddd

            position: relative

        }

    </style>

    <script type="text/javascript">

        function Choose(){

            var mine = this

            this.selections =[]

            this.init = function(rows,cloumns,where){

                var ul = document.createElement("ul")

                var siteNum=1

                for(var i =0i<cloumnsi++){

                    for(var j=0j<rowsj++){

                        var li = document.createElement("li")

                        li.style.listStyle = "none"

                        li.style.width = "50px"

                        li.style.height = "30px"

                        li.style.border ="1px solid #000"

                        li.style.color ="#000"

                        li.style.background ="green"

                        li.style.fontSize ="30px"

                        li.style.lineHeight ="30px"

                        li.style.textAlign ="center"

                        li.style.cursor ="pointer"

                        li.style.marginLeft = (j*52 +10) +"px"

                        if(j>0){

                            li.style.marginTop =  -32 +"px"

                        }else{

                            li.style.marginTop =  0 +"px"

                        }

                        li.textContent = siteNum++

                        ul.appendChild(li)

                    }

                }

                ul.onclick = function(e){

                    var target = e.target || e.srcElement

                    if(target.tagName.toUpperCase() ==='LI'){

                        if('red'===target.style.color){

                            target.style.background ="green"

                            target.style.color= "#000"

                           for(var i=0i< (mine.selections.length)i++){

                               if(mine.selections[i] === target.textContent){

                                   mine.selections.splice(i,1)

                               }

                           }

                        }else{

                            target.style.background = "#ccc"

                            target.style.color ='red'

                            mine.selections.push(target.textContent)

                        }

                    }

                }

                var toWhere = where || document.body

                toWhere.appendChild(ul)

            }

        }

        window.onload = function(){

            var chooser = new Choose()

            chooser.init(20,10)

           document.getElementById("test").onclick = function(){

               console.log(chooser.selections)

           }

        }

    </script>

</head>

<body>

<button id="test">测试</button>

</body>

</html>

首先。action的属性是在form上的。但是要想在另一个页面去获取到表单传递过来的值。需要后台实现。例如php

<form action="get.php" method="get">

<input value="123" name="inputValue">

<button type="submit"></button>

</form>

get.php代码

<?php

echo $_GET["inputValue"]这样就可以输出传递过来的值。

?>

另一种方式就是用到cookie的存储方式去传递。一个页面存值。另一个页面去读取。

同样的方式还有localstorage的本地存储方式去传递。