zoukankan      html  css  js  c++  java
  • Jquery把获取到的input值转换成json

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <title> jquery把获取到的input值转换成json</title>
     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
     <link rel="stylesheet" type="text/css" href="" />
     <style type="text/css">
     .box{width:500px;height:500px;border:1px solid #e5e5e5;}
     </style>
     <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
     </head>
     <body>
     <div class="box">
        姓名:<input type="text" value="" name="username" /> <br/><br/>
        年龄:<input type="text" value="" name="age" /> <br/><br/>
        生日:<input type="text" value="" name="birth" /> <br/><br/>
        城市:<input type="text" value="" name="city" /> <br/><br/>
        省份:<input type="text" value="" name="provicen" /> <br/><br/>
        性别:<input type="text" value="" name="sex" /> <br/><br/>
        <input type="button" value="提交" class="comment">
     </div>
     <script type="text/javascript">
      $(document).ready(function(){
        $(".comment").click(function(){
          parArr = {};
           var username = $('input[name=username]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.username = username;
           var age = $('input[name=age]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.age = age;
           var birth = $('input[name=birth]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.birth = birth;
           var city = $('input[name=city]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.city = city;
           var provicen = $('input[name=provicen]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.provicen = provicen;
           var sex = $('input[name=sex]').val().replace(/(^s*)|(s*$)/g, "");
           parArr.sex = sex;
           var params = JSON.stringify(parArr);
           alert(params);
        });
      });
     </script>
     </body>
    </html>
  • 相关阅读:
    转:一道笔试题-将int型数组强制转换为char*,再求strlen,涉及大小端
    转:git windows中文 乱码问题解决汇总
    git教程1
    刘汝佳黑书 pku等oj题目
    C/C++ qsort()快速排序用法
    char s[]字串和char *s字串有什麼区别?
    c语言‘’ ,‘0’, “0” ,0之间的区别
    带符号的char类型取值范围为什么是-128——127
    c语言memset详解
    【线性规划与网络流24题】孤岛营救问题 分层图
  • 原文地址:https://www.cnblogs.com/h-c-g/p/15075747.html
Copyright © 2011-2022 走看看