We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
最近项目中有个需求,就是查询条件可以动态追加,也就是有个按钮,点击后增加查询条件,比如:查询条件中有个select,点击添加,就多一个select。
于是出现的问题是,添加一个select,option的默认值就是上一个select中选中的值,而不是默认选择第一个!
实现思路大致为:
大致实现思路是:
var ary = { type1 : [{ id : 1, name : 'name1' },{ id : 2, name : 'name2' }], type2 : [{ id : 3, name : 'name3' },{ id : 4, name : 'name4' }] }; var vm1 = $.extend({},ary);
页面上使用的是MVVM框架 avalon 的双工绑定 duplex。
avalon
duplex
问题就出现在:
var vm1 = $.extend({},ary);
看下文的分析。
先来看看 jQuery.extend 的用法:
jQuery.extend
//通过递归的方式,将a对象的所有属性复制到b对象上 $.extend(true, b, a); //如果不使用递归,而是浅拷贝,则: $.extend(b, a);
vm1
ary
所以,这个问题最根本的解决办法就是使用深拷贝,即:
var vm1 = $.extend(true,{},ary);
Object.assign
The text was updated successfully, but these errors were encountered:
sunmaobin
No branches or pull requests
经验分享-jQuery.extend使用注意
问题
最近项目中有个需求,就是查询条件可以动态追加,也就是有个按钮,点击后增加查询条件,比如:查询条件中有个select,点击添加,就多一个select。
于是出现的问题是,添加一个select,option的默认值就是上一个select中选中的值,而不是默认选择第一个!
原因
实现思路大致为:
大致实现思路是:
页面上使用的是MVVM框架
avalon
的双工绑定duplex
。问题就出现在:
看下文的分析。
分析
先来看看
jQuery.extend
的用法:vm1
赋值的时候,其实是ary
对象的一级节点的引用!ary
对象。ary
模板的时候,其实已经被更改了。所以,这个问题最根本的解决办法就是使用深拷贝,即:
补充
Object.assign
也是浅拷贝,请注意使用。Object.assign
的深拷贝,可以自己写方法jQuery.extend
的源代码,jquery源码中搜索:jQuery.extend
The text was updated successfully, but these errors were encountered: