输入类型编号如何检查更改事件中是否按下了向上箭头或按下向下箭头(鼠标单击)
我有一个输入类型号
现在检查我使用的这个元素的变化
$(".item:input").bind('change', function (e) { //doing something }
现在在这个函数中如何检查是否按下了该元素的向上箭头或向下箭头
我正在寻找类似的东西
$(".item:input").bind('change', function (e) { if(this up arrow pressed){ print(up); }else{ print(down); } }
我该怎么做呢??
您可以将旧值存储到输入的属性中。 试试这个
$(document).ready(function(){ // assign oldVal data attribute at once on document ready $(".item:input").data('oldVal', $(".item:input").val()); $(".item:input").change(function(){ //get the newVal on change var newVal = $(this).val(); // get the oldVal from data attribute var oldVal = $(this).data('oldVal'); // do stuff you need here if ( newVal > oldVal) { //UP arrow pressed } else { //DOWN arrow pressed } console.log( 'newVal is ' + newVal + ' oldVal was ' + oldVal); //store the newVal as the oldVal for the next change $(this).data('oldVal', newVal) }) .focus(function(){ // assign oldVal data attribute on input focus $(this).data('oldVal', $(this).val()); }); });
为你做了一个小提琴
检查一下:
$(document).ready(function(){ $(".item:input").bind('keyup change click', function (e) { //doing something if(e.keyCode == 38) { //Enter keycode alert("up") }else if(e.keyCode == 40){ alert("down"); } });
});
演示: http : //plnkr.co/edit/CjSG5VVYFZXr2tEkna5x?p=preview
看看这个小提琴吧。 还没完成。 但你会明白怎么做.. 小提琴
$(".item:input").bind('keyup click', function (e) { //doing something if(e.keyCode == 38) { //Enter keycode alert("up") }else if(e.keyCode == 40){ alert("down"); } else { $("input").change(function() { if (a> $("input").val()) { alert("down"); a = $("input").val(); } else { a = $("input").val(); alert("up"); } }); } });