码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-10-19 19:55:49      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

<%@ 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 = 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="发射" onclick="test();"/> </form> </body> </html>

 

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

标签:

原文地址:http://www.cnblogs.com/Answer900310/p/5978132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!