Jquery把獲取到的input值轉(zhuǎn)換成json
更新時間:2017年05月15日 09:39:18 作者:yangzailu1990
本篇文章主要介紹了Jquery把獲取到的input值轉(zhuǎn)換成json的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
話不多說,請看代碼:
<!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值轉(zhuǎn)換成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>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
JS正則RegExp.test()使用注意事項(不具有重復(fù)性)
這篇文章主要介紹了JS正則RegExp.test()使用注意事項,結(jié)合實例形式分析了RegExp.test()方法的功能與用法,以及針對不能重復(fù)調(diào)用的解決方法,需要的朋友可以參考下2016-12-12
javascript高級程序設(shè)計第二版第十二章事件要點總結(jié)(常用的跨瀏覽器檢測方法)
javascript高級程序設(shè)計第二版第十二章事件要點總結(jié)(常用的跨瀏覽器檢測方法),需要的朋友可以參考下2012-08-08

