카테고리 없음
[javascript] jquery 입력에서 이름 필터링
행복을전해요
2020. 12. 9. 11:46
This works:
$('input[name^="record_"]').each(function() {
total += this.value * (parseInt(this.name.substr(7),10)<4 ? 8 : 1);
});
DEMO: http://jsfiddle.net/WKb7n/1/
It also avoids creating unnecessary variables or lookups inside the loop, which should make it more efficient.
-------------------다음과 같이 할 수 있습니다.
$('input[name^="record_"]').map(function() {
var name = this.name;
var number = name.replace('record_', '')
if (number > 3){
total += $(this).val() * 1;
}else{
total += $(this).val() * 8;
}
console.log(number);
});
-------------------이 시도:
$( 'input[name^="record_"]' ).each(function () {
var n = +this.name.split( '_' )[1];
total += this.value * ( n > 3 ? 1 : 8 );
});
라이브 데모 : http://jsfiddle.net/QAMcA/1/
출처
https://stackoverflow.com/questions/7414948