Foros del Web » Creando para Internet » HTML »

[SOLUCIONADO] Incremento y decremento ciclico en <input type="number">

Estas en el tema de Incremento y decremento ciclico en <input type="number"> en el foro de HTML en Foros del Web. Hola a todos, tengo el siguiente codigo, por ejemplo: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código HTML: Ver original < input type = "number" max = "31" min = ...
  #1 (permalink)  
Antiguo 26/05/2016, 04:08
 
Fecha de Ingreso: mayo-2013
Mensajes: 169
Antigüedad: 11 años
Puntos: 25
Incremento y decremento ciclico en <input type="number">

Hola a todos,
tengo el siguiente codigo, por ejemplo:
Código HTML:
Ver original
  1. <input type="number" max="31" min="1" value="1" step="1">

Todo bien, pero lo que quiero que si se le pulsa la flecha hacia abajo del input cuando es 1, se pase a 31. Y cuando está en 31 y se le de la flecha de incremento pase a 1. En pocas palabras que sea un incremento y decremento ciclico.

¿Conoce alguien una forma de hacerlo? ¿Algún atributo para hacer que sea ciclico? He estado mirando documentación y no he visto nada.

¿O tengo que hacerlo por javascript? En caso de hacerlo con Javascript, "¿qué eventos saltan cuando incrementa y decrementa?"

gracias, un saludo.
  #2 (permalink)  
Antiguo 26/05/2016, 07:50
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 10 meses
Puntos: 263
Respuesta: Incremento y decremento ciclico en <input type="number">

con un imput number no creo, vas a necesitar algo como javascript para ello:

Código HTML:
Ver original
  1. <div class="col-lg-6">
  2.   <div class="input-group">
  3.     <span class="input-group-btn">
  4.       <button class="btn btn-default" id="carga" onClick="sube()" type="button">Carga</button>
  5.     </span>
  6.     <input type="text" id="contador" class="form-control" placeholder="1" value="1">
  7.    <span class="input-group-btn">
  8.      <button class="btn btn-default" id="descarga" onClick="baja()" type="button">Descarga</button>
  9.     </span>
  10.   </div>
  11. </div>

Código Javascript:
Ver original
  1. function sube(){
  2.   var num = $("#contador").val();
  3.  
  4.   if(num == 31){
  5.     $("#contador").val(1);
  6.   }else{
  7.     $("#contador").val(parseInt(num)+1);
  8.   }
  9. }
  10.  
  11. function baja(){
  12.   var num = $("#contador").val();
  13.  
  14.   if(num == 1){
  15.     $("#contador").val(31);
  16.   }else{
  17.     $("#contador").val(parseInt(num)-1);
  18.   }
  19.  
  20. }

Aquí una muestra que hice: http://codepen.io/anon/pen/QELBxR

Saludos, espero te sirva.

NOTA: El input es un input group de bootstrap.
__________________
[email protected]
HITCEL
  #3 (permalink)  
Antiguo 27/05/2016, 07:07
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Incremento y decremento ciclico en <input type="number">

como se te comenta, has de hacerlo con javascript.
en este código está incluido el uso de las teclas (flochas) del teclado
Cita:
<!DOCTYPE html>
<html dir="ltr" lang="es-es">
<head>
<title></title>
<meta charset="utf-8" />
<style type="text/css">
* {
margin: 0;
padding: 0;
position: relative;
}



body {
width: 100%;
height: 100%;
font-size: 100%;
}



form {
width: 49%;
height: auto;
margin: 0 auto;
overflow: hidden;
}




input.spin {
width: 15%;
margin: 0 7%;
text-shadow: 0 0 0;
font: 2.4em Verdana;
background-position: right;
background-repeat: no-repeat;
background-image: url(./EmularSPINbutton/imgdefecto.gif); /*http://imageshack.com/a/img856/9611/58u7.gif*/
background-size: .66em 1.2em;
background-color: rgb(255, 255, 255);
border: 1px solid rgb(228, 221, 221);
outline: 0;
}


input.spin:hover {
background-image: url(./EmularSPINbutton/spin.gif); /*http://imageshack.com/a/img853/3977/igp.gif*/
}
</style>
<script type="text/javascript">
var inputNumber = {

Evento : function(elemento, nomevento, fnc) {

elemento.addEventListener(nomevento, fnc, false);
},



cssComputado : function(obj, styleProp) {

if (obj == null) { return 0; }

var valor = window.getComputedStyle(obj, null)[styleProp];

return valor;
},



posCursor : function(evt) {

var ev = evt || window.event;

posX = ev.offsetX || ev.layerX || 0;
posY = ev.offsetY || ev.layerY || 0;

return {x:posX, y:posY};
},


WObj : 0,
WImg : 0,
HImg : 0,


initinputNumber : function() {

var sboton = document.querySelectorAll('.spin');
inputNumber.WObj = parseInt(inputNumber.cssComputado(sboton[0], 'width'), 10);

var bgSize = inputNumber.cssComputado(sboton[0], 'backgroundSize').split(' ');
inputNumber.WImg = parseInt(bgSize[0], 10);
inputNumber.HImg = parseInt(bgSize[1], 10);

Array.prototype.forEach.call(sboton, function(spin) {

inputNumber.initinputNumberEventos(spin);

});
},



initinputNumberEventos : function(spin) {

inputNumber.Evento(spin, 'mousemove', function(event) {

var pos = inputNumber.posCursor(event);

if (pos.x >= (inputNumber.WObj - inputNumber.WImg) + 4) {

this.style.cursor = 'default';

} else {

this.style.cursor = '';
}
});


inputNumber.Evento(spin, 'click', function(event) {

var pos = inputNumber.posCursor(event);

if (pos.x >= (inputNumber.WObj - inputNumber.WImg) + 4) {

var _this = this;

if (pos.y <= parseInt((inputNumber.HImg / 2), 10)) {

inputNumber.spinButton(true, _this, _this.getAttribute('data-min'), _this.getAttribute('data-max'), _this.getAttribute('data-step'));

} else {

inputNumber.spinButton(false, _this, _this.getAttribute('data-min'), _this.getAttribute('data-max'), _this.getAttribute('data-step'));
}

}

});


inputNumber.Evento(spin, 'keydown', function(event) {

var pos = inputNumber.posCursor(event);

if (pos.x < (inputNumber.WObj - inputNumber.WImg) + 4) {

var _this = this;

inputNumber.flechas(event, _this, _this.getAttribute('data-min'), _this.getAttribute('data-max'), _this.getAttribute('data-step'));
}

});
},




spinButton : function(bol, input, min, max, step) {

var inputValue = parseInt(input.value, 10) || 0;

var cantidad = (bol) ? inputValue + parseInt(step, 10) : inputValue - parseInt(step, 10);

if (cantidad < min) { cantidad = max; input.value = max; }
if (cantidad > max) { cantidad = min; input.value = min; }

input.value = cantidad;
},


// FLECHAS TECLADO

flechas : function(evt, input, min, max, step) {

var keyCode = (evt) ? evt.keyCode : event.charCode;

if (keyCode == 40) {

inputNumber.spinButton(false, input, min, max, step);

} else if (keyCode == 38) {

inputNumber.spinButton(true, input, min, max, step);

} else {

return false;
}
}

// FLECHAS TECLADO
}




inputNumber.Evento(document, 'DOMContentLoaded', inputNumber.initinputNumber);
</script>
</head>
<body>

<form>

<input type="text" class="spin" data-min="0" data-max="100" data-step="5" value="0" />
<input type="text" class="spin" data-min="0" data-max="50" data-step="10" value="0" />
<input type="text" class="spin" data-min="20" data-max="40" data-step="1" value="0" />

</form>

</body>
</html>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #4 (permalink)  
Antiguo 30/05/2016, 03:29
 
Fecha de Ingreso: mayo-2013
Mensajes: 169
Antigüedad: 11 años
Puntos: 25
Respuesta: Incremento y decremento ciclico en <input type="number">

Gracias, como me temía no puedo usar el input number de html5

Gracias por los codigos queda solucionado.

Etiquetas: form, incremento, input, todo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:35.