jquery input number [duplicate]

淺唱寂寞╮ 提交于 2019-12-21 17:35:22

问题


Possible Duplicate:
Is it possible to customize an input field for amounts with +- buttons?

how to make + (plus) and - (minus) value buttons for

<input type="number" class="numbertype" value="10"> ?

I think it's possible to do with jQuery or maybe simple javascipt, but i don't know how..

I want do something like this :

when you push on + button, value will be bigger for 1 (0,12,3,4,5,6.... 10000)

when you push on - button, value will be smaller for 1 (10,9,8,7,6,5,4... 0)


回答1:


$("#minus,#plus").click(function(){
    var value = parseInt($(".numbertype").val(), 10);
    $(".numbertype").val(value + $(this).is("#minus") ? -1 : 1);
});

I just wanted to see how to do it myself. Here's a toggle that will keep going on mouse down:

var i = null;
var d = 0;
var $numbertype = null;

function ToggleValue() {
    $numbertype.val(parseInt($numbertype.val(), 10) + d);
}

$(function() {
    $numbertype = $(".numbertype");

    $("#minus,#plus").mousedown(function() {
        d = $(this).is("#minus") ? -1 : 1;
        i = setInterval(ToggleValue, 100);
    });

    $("#minus,#plus").on("mouseup mouseout", function() {
        clearInterval(i);
    });
});

working example: http://jsfiddle.net/M4BAt/5/




回答2:


You can use the kendo.iu it has predefined control satisfying your wishes even more.It is very easy to implement and works great. I have already used it. No complains.

http://demos.kendoui.com/web/numerictextbox/index.html




回答3:


$('#plus_but').click(function(){

        var val = $('.numbertype').val();

        var new_val = parseInt($('.numbertype').val(),10) + 1 ;

       $('.numbertype').val(new_val); 

});


$('#minus_but').click(function(){

        var val = $('.numbertype').val();

        var new_val = parseInt($('.numbertype').val(),10) - 1 ;

       $('.numbertype').val(new_val); 

});



回答4:


If you substitute the images you want for the plus/minus buttons, this would work:

<span class="plusminusunit">
    <span class="minus">-</span> 
    <input type="number" class="numbertype" value="10">
    <span class="plus">+</span>
</span>

$(".plus").click(function() {
    var item = $(this).closest(".plusminusunit").find("input");
    item.val(Number(item.val()) + 1); 
});
$(".minus").click(function() {
    var item = $(this).closest(".plusminusunit").find("input");
    item.val(Number(item.val()) - 1); 
});

Working example: http://jsfiddle.net/jfriend00/FxEv3/

With images, the HTML would look like this (using your image URLs):

<span class="plusminusunit">
    <img class="minus" src="http://lt.tonybet.com/assets/shared/mikro_tv-50ee396d59d5c38f979ad3fd93578f4f.png"> 
    <input type="number" class="numbertype" value="10">
    <img class="plus" src="http://lt.tonybet.com/assets/shared/mikro_tv-50ee396d59d5c38f979ad3fd93578f4f.png">
</span>


来源:https://stackoverflow.com/questions/9112970/jquery-input-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!