I have a form with many input=text and I have neraby two image (-,+) I want to increase or decrease the previous input text. I don't want to put into the input text an id because I can have a lot of this line with input text and minus or plus. With my code the minus button works but the plus button not why?
My jQuery code
$('.minus').click(function(){
var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
var num = $(this).prev('input').val();
if(intRegex.test(num) || floatRegex.test(num)) {
if (num>0){
$(this).prev('input').val(num-1);
}
}
else{
//non è un numero
}
});
$('.plus').click(function(){
var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
var num = $(this).prev.prev('input').val();
if(intRegex.test(num) || floatRegex.test(num)) {
$(this).prev.prev('input').val(num+1);
}
else{
//non è un numero
}
});
HTML:
<input type="text" name="qta" id="qta" style="width:50px;"/>
<img src="img/shop/meno.jpg" alt="" class="minus" style="cursor:pointer"/>
<img src="img/shop/piu.png" alt="" class="plus" style="cursor:pointer"/>
var num = $(this).prev().prev('input').val();
and
$(this).prev().prev('input').val(num+1);
You missed a ()
twice
anyway I would wrap every input and images in their own wrapper and I would find the input with $(this).parent().find('input')
use this to go previous
var num = $(this).prev().prev('input').val();
if you want to go next use this
var num = $(this).next().next('input').val();
if you have only one input , you can use the ID of that input
$('#qta').val(num+1)
and
$('#qta').val(num-1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.