javascript - js关于函数内向外传递变量的问题
大家讲道理
大家讲道理 2017-04-10 16:33:35
0
6
575

项目中一个表单有很多项,获取修改过的表单项的组成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的函数内向外传递变量,网上搜索匿名函数闭包可以实现,但是以上的情况在绑定事件的时候可以吗?就是什么情况下适用使用闭包可以向外传递变量?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(6)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!