简单知识点实例之一:如何将各个单一获取的数据存储为数组对象并将其取出

来源:互联网 发布:vue.js 动态添加dom 编辑:程序博客网 时间:2024/05/28 18:43

一、将获取的值存为数组或数组对象

(1)存为数组(例如所有怪物的id值可以存为数组)

是以逗号隔开的,建议用到存单个特定值时用(如光存id值时)

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="js/jquery-1.11.1.js"></script>    <script>        window.onload = function () {            //获取各个输入框的值            var ids = $("#inputId").val();            //定义一个空数组            var arryList = new Array();            //将获取的值存为数组            arryList.push(ids);            alert(arryList);        }    </script></head><body>    <input type="text" value="123456" id="inputId"/></body></html>


取值:直接可以用for循环对比里面的值即可

(2)存为对象(例如一个怪物的所有属性值可以存为对象:id,type,name)

如果有多条数据,会以序号分别列出来。建议以此去多个值混合的多条数据。这样取值方便很多。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="js/jquery-1.11.1.js"></script>    <script>        window.onload = function () {            //获取各个输入框的值            var ids = $("#inputId").val();            var names = $("#inputName").val();            var types = $("#inputType").val();            //定义一个对象            var obj = new Object();            //将获取的值存入对象            obj.id = ids;            obj.name = names;            obj.type = types;            console.log(obj);        }    </script></head><body>    <input type="text" value="123456" id="inputId"/>    <input type="text" value="我是输入框的名字" id="inputName"/>    <input type="text" value="0" id="inputType"/></body></html>

取值:XXX.id   XXX.name   XXX.type即可(i是序号)

(3)存为数组对象(例如多个怪物的多个属性值可以存为数组对象:id,name,type)

何为数组对象?就是数组包裹着对象的数据形式

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="js/jquery-1.11.1.js"></script>    <script>        window.onload = function () {            //获取各个输入框的值            var ids = $("#inputId").val();            var names = $("#inputName").val();            var types = $("#inputType").val();            //定义一个空数组            var arryList = new Array();            //定义一个对象            var obj = new Object();            //将获取的值存入对象            obj.id = ids;            obj.name = names;            obj.type = types;            arryList.push(obj);            console.log(arryList);        }    </script></head><body>    <input type="text" value="123456" id="inputId"/>    <input type="text" value="我是输入框的名字" id="inputName"/>    <input type="text" value="0" id="inputType"/></body></html>

取值:XXX[i].id   XXX[i].name   XXX[i].type即可(i是序号)

阅读全文
0 0