product-details.js
5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
$(function() {
var Goods = {
initialize: function() {
this.initGoodsNum();
this.bindEvents();
},
bindEvents: function() {
var _this = this;
$('.j_goodsInfo').on('click', '.reduce', function() {
if ($(this).hasClass('disabled')) {
return;
}
_this.reduceNum($(this));
}).on('click', '.add', function() {
if ($(this).hasClass('disabled')) {
return;
}
_this.addNum($(this));
}).on('change', '.m-input-number input', function() {
_this.verInputVal($(this));
});
$('.props').on('click', '.btn', function() {
$(this).addClass('selected').siblings().removeClass('selected');
});
$('.submitBtn').on('click', function(e){
console.log($(this).closest('.goods-data').find('.j_goodsInfo'));
$(this).closest('.goods-data').find('.j_goodsInfo').find('.props').find('.error-box').toggle(!_this.verAttrSelected($(this).closest('.goods-data').find('.j_goodsInfo')));
if(!_this.verAttrSelected($(this).closest('.goods-data').find('.j_goodsInfo'))){
e.preventDefault();
}
});
},
initGoodsNum: function() {
var _this = this;
$('.j_goodsInfo').each(function() {
var min = _this.getMinNum($('.j_goodsInfo').children());
var max = _this.getMaxNum($('.j_goodsInfo').children());
$(this).find('.m-input-number input').val(min).siblings('.reduce').addClass('disabled');
});
},
getGoodsNum: function(input) {
var num = +$.trim(input.val());
var min = +this.getMinNum(input);
var max = +this.getMaxNum(input);
if (!/^\d+$/.test(num)) {
num = 0;
}
num = num <= min ? min : num;
num = num >= max ? max : num;
return num;
},
getMinNum: function(input) {
if (input.closest('.j_goodsInfo').find('.small-sales').length <= 0) {
return input.closest('.j_goodsInfo').find('.j_minNum').text();
}
return input.closest('.j_goodsInfo').find('em[data-range]').eq(0).attr('data-range').split(',')[0];
},
getMaxNum: function(input) {
return input.closest('.j_goodsInfo').find('.j_maxNum').text();
},
addNum: function(target) {
var input = target.siblings('input');
var num = this.getGoodsNum(input);
var min = this.getMinNum(input);
var max = this.getMaxNum(input);
num += 1;
num = num <= min ? min : num;
num = num >= max ? max : num;
input.val(num);
this.updateBtnStyle(target, num);
},
reduceNum: function(target) {
var input = target.siblings('input');
var num = this.getGoodsNum(input);
var min = this.getMinNum(input);
var max = this.getMaxNum(input);
num -= 1;
num = num <= min ? min : num;
num = num >= max ? max : num;
input.val(num);
this.updateBtnStyle(target, num);
},
verInputVal: function(target) {
var num = this.getGoodsNum(target);
target.val(num);
this.updateBtnStyle(target, num);
},
updateBtnStyle: function(target, num) {
var min = this.getMinNum(target);
var max = this.getMaxNum(target);
target.closest('.j_goodsInfo').find('.add').toggleClass('disabled', num == max);
target.closest('.j_goodsInfo').find('.reduce').toggleClass('disabled', num == min);
this.tabPriceStyle(target, num);
},
tabPriceStyle: function(target, num) {
var wrap = target.closest('.j_goodsInfo');
if (target.closest('.j_goodsInfo').find('.small-sales').length <= 0) {
return;
}
wrap.find('em[data-range]').each(function() {
var prices = $(this).attr('data-range').split(',');
if (prices.length == 2) {
if (num >= +prices[0] && num <= +prices[1]) {
$(this).closest('.cell').addClass('current').siblings().removeClass('current');
return false;
}
} else {
if (num >= prices[0]) {
$(this).closest('.cell').addClass('current').siblings().removeClass('current');
return false;
}
}
})
},
verAttrSelected:function(wrap){
var isOk = true;
wrap.find('.props .info-attr').each(function(){
var ok = $(this).find('.selected').length > 0;
$(this).find('.title').toggleClass('font-red', !ok);
$(this).closest('.props').find('.error-box').toggle(!ok);
isOk = isOk ? ok : isOk;
if(!ok){
return false;
}
});
if(!isOk){
return;
}
return isOk;
}
};
Goods.initialize();
});