기본적으로 증가 및 감소 버튼을 수행하려고합니다. 텍스트 값이 24에 도달하면 증가를 멈춰야합니다.JavaScript 증분 및 감소 버튼
<input type='button' value='-' class='qtyminus' field='quantity' />
<input type='text' name='quantity' value='0' class='qty' />
<input type='button' value='+' class='qtyplus' field='quantity' />
내 CSS : 다음은 HTML 코드입니다
.qty {
width: 40px;
height: 25px;
text-align: center;
}
input.qtyplus {
width:25px;
height:25px;
}
input.qtyminus {
width:25px;
height:25px;
}
그리고 내 자바 스크립트 :
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
});
});
여기 내 fiddle입니다. 나는이 바이올린을 연구함으로써 얻는다. 나는 거기에 더하기 버튼과 같은 전체를 스타일링하는 방법이 있는지 궁금하다. 그 다음에는 텍스트 상자와 마이너스 버튼이 뒤 따른다.
또한 JavaScript로 플러스 마이너스 한도를 설정할 수있는 방법이 있습니까? if ekse 문을 설정했기 때문에 작동하지 않습니다.
단순히 if 문을 넣어 .. 당신을 도움이되기를 바랍니다 귀하의 경우 (! 때는 isNaN (currentval)) 당신은'형식을 시도해 봤어 확인 미만 23 – VoronoiPotato
을 년대 currentval을 확인하고 만들기 = '번호'? – hjpotter92
예, 작동합니다. 그렇다면 CSS는 어때요? 단추와 텍스트 상자를 세로로 정렬하는 방법을 모르기 때문입니다. –