项目中一个表单有很多项,获取修改过的表单项的组成json数据ajax提交修改的功能实现,比如:
<input type="text" class="inputxt" name="a1" value="111">
<input type="text" class="inputxt" name="a2" value="222">
<input type="text" class="inputxt" name="a3" value="333">
...
a[n]个表单项
<script>
var modjson;
$('.inputxt').each(function(){
$(this).bind('change',function(){
modjson+=","+$(this).attr("name")+":"+$(this).val();
});
});
if(modjson){
modjson=modjson.substr(1);
modjson="{"+modjson+"}";
console.log(modjson);//获取修改过的表单项json数据,类如{a1:'555',a3:'77'}
}
</script>
这样肯定不行,js的函数内向外传递变量,网上搜索匿名函数闭包可以实现,但是以上的情况在绑定事件的时候可以吗?就是什么情况下适用使用闭包可以向外传递变量?
1)你为什么要没change一次的都要记录一次呢?
2)你在数据加载后记录表单项的原始值,等到你需要提交数据的时候再获取表单项的最后结果,和原始值做一次比较,获取变化后的值一并提交不就好了
还是说有其它的业务需求?