封装js插件实现一个表单序列化的值,转为JSON格式
程序员文章站
2022-05-11 20:29:42
...
//了解封装的格式:
1.原生js封装方式。
;(function () {
//代码块
})();
2.jQuery的封装方式
//声明一个函数并立即执行函数,用$(形参)代替jQuery实参
;(function ($) {
//代码块
})(jQuery);
进行封装:封装一个表单序列化。
;(function(){
jQuery.prototype.serializeObject=function(){
var obj=new Object();
$.each(this.serializeArray(),function(index,param){
if(!(param.name in obj)){
obj[param.name]=param.value;
}
});
return obj;
};
})();
或者:
;(function($){
jQuery.prototype.serializeObject=function(){
var obj=new Object();
$.each(this.serializeArray(),function(index,param){
if(!(param.name in obj)){
obj[param.name]=param.value;
}
});
return obj;
};
})(jQuery);
调用:
a.表单代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link href="../css/mui.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../css/components.min.css"/>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
</head>
<body>
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">设置信息</h1>
</header>
<div class="mui-content">
<div class="panel panel-info" style="margin-top: 26px">
<div class="panel-heading">
<h3 class="panel-title" style="">基本信息</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" method="post" id="updateBsform" role="form">
<input type="hidden" name="Id" v-bind:value="message.blogSetId">
<input type="hidden" name="Token" v-bind:value="message.token">
<div class="form-group" style="margin-top: 5px">
<label for="" class="col-md-2 control-label">博客昵称</label>
<div class="col-md-6">
<div class="input-icon right">
<input type="text" class="form-control" id="bName" name="BlogName" v-bind:value="personInfo.blogName" placeholder="只有我最帅">
</div>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">个性签名</label>
<div class="col-md-6">
<div class="input-icon right">
<input type="text" class="form-control" id="bPersonality" name="Motto"v-bind:value="personInfo.motto" placeholder="我没有个性,不需要签名">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="button" id="updateBs" class="btn green">确定</button>
<span style="display: inline-block; margin-left: 10px; color: red"></span>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="../js/mui.js"></script>
<script src="../js/jquery-2.1.0.js"></script>
<script src="../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/serializeObject.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8" >
mui.init();
mui.plusReady(function () {
var ready=plus.webview.currentWebview();
vm.message=ready;
updateBsform(ready.userName);
})
let vm=new Vue({
el:"#updateBsform",
data:{
message:"",
personInfo:""
}
})
function updateBsform(userName){
mui.get('http://m.tnblog.net/api/v1/UserInfo/'+userName,function(data){
vm.personInfo=data.value;
});
}
// jQuery.prototype.serializeObject=function(){
// var obj=new Object();
// $.each(this.serializeArray(),function(index,param){
// if(!(param.name in obj)){
// obj[param.name]=param.value;
// }
// });
// return obj;
// };
document.getElementById("updateBs").addEventListener("tap",function(){
var valaa=jQuery("form").serializeObject();
console.log(JSON.stringify(valaa));
//序列化表单
// var y=$("form").serializeArray();
// var obj=new Object();
// $.each(y,function(index,param){
// if(!(param.name in obj)){
// obj[param.name]=param.value;
// }
// });
// console.log(JSON.stringify(obj))
// $.post("http://m.tnblog.net/api/v1/BlogSet",{data},function(result){
// console.log(JSON.stringify(result));
// })
});
</script>
</body>
</html>
重要部分:
var valaa=jQuery("form").serializeObject();
console.log(JSON.stringify(valaa));
也可以这样写
var valaa1=$("form").serializeObject();
console.log(JSON.stringify(valaa1));
结果为:
1页面展示:
点击确定后,输出结果为: