首页 > 代码库 > 通过js动态生成对象,并给对象赋值

通过js动态生成对象,并给对象赋值

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script   src="http://code.jquery.com/jquery-1.11.0.min.js"   integrity="sha256-spTpc4lvj4dOkKjrGokIrHkJgNA0xMS98Pw9N7ir9oI="   crossorigin="anonymous"></script>
</head>
<body>
    <script type="text/javascript">
    //动态创建对象,并保存数据
    function saveData(inputName,fm){
        var obj = new Object();
        for(j=0;j<fm.length;j++){
            obj[fm[j]] =$("#"+inputName[j]).val();
        }
      var data = http://www.mamicode.com/JSON.stringify(obj,fm);//将对象转换成json
   alert(data);
} function test(){ var inputName = new Array(); inputName[0] = "textinput1"; inputName[1] = "textinput2"; inputName[2] = "textinput3"; var fm = new Array(); fm[0] = "name"; fm[1] = "number"; fm[2] = "userSchool"; var result = saveData(inputName, fm); alert(result); } </script> <form> <input name="textinput1" id="textinput1" type="text"> <input name="textinput2" id="textinput2" type="text"> <input name="textinput3" id="textinput3" type="text"> <input name="btnGo" id="btnGo" type="button" value="http://www.mamicode.com/发射" onclick="test();"/> </form> </body> </html>

 

通过js动态生成对象,并给对象赋值