Javascript 我想删除多余的代码并使用一个单独的Jquery函数
这是我试图删除冗余代码并将代码移动到单独函数的代码Javascript 我想删除多余的代码并使用一个单独的Jquery函数,javascript,jquery,Javascript,Jquery,这是我试图删除冗余代码并将代码移动到单独函数的代码 //Adding Infotypes to filter and checks whether any infotype option is selected if(this.$infoOptions.val() != null){ var infotypelength = this.$infoOptions.val().length; var l=0,j;
//Adding Infotypes to filter and checks whether any infotype option is selected
if(this.$infoOptions.val() != null){
var infotypelength = this.$infoOptions.val().length;
var l=0,j;
//Condition to check empty input type
if( infotypelength > 0){
var infotypeList = this.$infoOptions.val();
for(j = 0; j < infotypelength; j++) {
//It checks whether already option is selected and prevents adding to filter if its duplicate.
if(($.inArray( $('#infoOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#infoOptions').select2('data')[j].text);
if(infotypeList[j].contains('_subgroup')){
var res = infotypeList[j].split("_");
this.aSubinfotype[l]=res[0];
l=l+1;
}
else
this.aInfotypes.push(infotypeList[j]);
}
}
}
}
//Adding Countries to filter
if(this.$countryOptions.val() != null){
var geoLength = this.$countryOptions.val().length;
//Condition to check empty input type
if( geoLength > 0){
var geoList = this.$countryOptions.val();
var l=0;
for(var j = 0; j < geoLength; j++) {
if(($.inArray( $('#countryOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#countryOptions').select2('data')[j].text);
if(geoList[j].contains('_subgroup')){
var res = geoList[j].split("_");
this.aSubgeotype[l]=res[0];
l=l+1;
}
else
this.aGeography.push(geoList[j]);
}
}
}
}
//将信息类型添加到筛选器并检查是否选择了任何信息类型选项
if(this.$infoOptions.val()!=null){
var infotypelength=this.$infoOptions.val().length;
var l=0,j;
//检查空输入类型的条件
如果(infotypelength>0){
var infotypeList=this.$infoOptions.val();
对于(j=0;j0){
var geoList=this.$countryOptions.val();
var l=0;
对于(var j=0;j
但我在将变量和缓存的选择器传递到其他函数时遇到了问题。有人能帮我吗?我不知道您的实现是如何完成的,但我真的认为您可以改进它,顺便说一句,您可以通过两种不同的方式减少代码: 电话:
myFunction({
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
});
或
第一种方法是一个接一个地传递数据,第二种方法是将数据传递到json对象。这样做不需要几个小时的帮助。。。提取两个块之间的差异数据,在参数中使用这些数据生成函数…感谢您的回复,我被下面的第1部分困住了。如何将缓存的选择器(我的意思是$('#infoOptions').select2('data'))作为参数传递给函数(),以便动态使用它…2。我想将该值推送到全局变量,而不是局部作用域数组中。。请帮我举个例子……非常感谢!这当然有助于我继续前进。。再次感谢!
// data = {option, filter, array, selector, subType}
var myFunction = function(data) {
if(data.option && data.option.val()){
var optList = data.option.val();
var optLength = optList.length;
//Condition to check empty input type
if( optLength > 0) {
var l = 0;
for(var j = 0; j < optLength; j++) {
if( ($.inArray( data.selector.select2('data')[j].text, filterList)) == -1 ){
data.filter.push(data.selector.select2('data')[j].text);
if(optList[j].contains('_subgroup')){
var res = optList[j].split("_");
data.subType[l]=res[0];
l=l+1;
} else {
data.array.push(optList[j]);
}
}
}
}
}
}
myFunction({
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
});
var data = {
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
}
myFunction(data);