Answer the question
In order to leave comments, you need to log in
How to simplify code with Angular.js?
Hello, friends.
Wrote the code, but it seems it can look much prettier.
The fact is that the first part duplicates the second part, only the variables change. I have 6000 lines of such code.
How to optimize. Thanks in advance.
//FIRST PART
$(function() {
var update12 = function() {
var val = +this.value;
var showFirst = [4].indexOf(val) >= 0;
var showSecond = [3,4,5,6,8].indexOf(val) >= 0;
$('#f_lr1gr2').toggle(showFirst);
$('#f_lr1grt').toggle(showSecond);
!showFirst && $('#f_lr1gr2 select').val(1).change();
!showSecond && $('#f_lr1grt select').val(1).change();
};
var update44 = function() {
var val = +this.value;
var showFirst = [8].indexOf(val) >= 0;
$('#f_lr1gr2el1').toggle(showFirst);
$('#f_lr1gr2el2').toggle(showFirst);
$('#f_lr1gr2el3').toggle(showFirst);
$('#f_lr1gr2el4').toggle(showFirst);
$('#f_lr1gr2el5').toggle(showFirst);
!showFirst && $('#f_lr1gr2el1 select').val(1).change();
!showFirst && $('#f_lr1gr2el2 select').val(1).change();
!showFirst && $('#f_lr1gr2el3 select').val(1).change();
!showFirst && $('#f_lr1gr2el4 select').val(1).change();
!showFirst && $('#f_lr1gr2el5 select').val(1).change();
};
$('#lr1rt').change(update12).change();
$('#lr1rt').change(update44).change();
});
//ВТОРАЯ ЧАСТЬ
$(function() {
var update112 = function() {
var val = +this.value;
var showFirst = [4].indexOf(val) >= 0;
var showSecond = [3,4,5,6,8].indexOf(val) >= 0;
$('#f_sp1gr2').toggle(showFirst);
$('#f_sp1grt').toggle(showSecond);
!showFirst && $('#f_sp1gr2 select').val(1).change();
!showSecond && $('#f_sp1grt select').val(1).change();
};
var update144 = function() {
var val = +this.value;
var showFirst = [8].indexOf(val) >= 0;
$('#f_sp1gr2el1').toggle(showFirst);
$('#f_sp1gr2el2').toggle(showFirst);
$('#f_sp1gr2el3').toggle(showFirst);
$('#f_sp1gr2el4').toggle(showFirst);
$('#f_sp1gr2el5').toggle(showFirst);
!showFirst && $('#f_sp1gr2el1 select').val(1).change();
!showFirst && $('#f_sp1gr2el2 select').val(1).change();
!showFirst && $('#f_sp1gr2el3 select').val(1).change();
!showFirst && $('#f_sp1gr2el4 select').val(1).change();
!showFirst && $('#f_sp1gr2el5 select').val(1).change();
};
$('#sp1rt').change(update112).change();
$('#sp1rt').change(update144).change();
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question