문제

Considering the following snippet

<input type="text" id="age" value="" maxlength="2" style="width: 30px;" />
<script type="text/javascript">
    $("#age").spinner({
        min: 1,
        max: 99,
        spin: function(event, ui) {
            $(this).change();
        }
    }).val(35);
    $("#age").change(function() {
        console.log($("#age").val());
    });
</script>

Why when onchange event occurs by pressing Up/Down key or clicking the spinner it log the old spinner value?

JS Fiddle

도움이 되었습니까?

해결책

The plugin takes some time to update the value. Try using the build in callback, ref: http://api.jqueryui.com/spinner/#event-spin

as a seperate event.

$("#age").on( "spin", function( event, ui ) { 
    console.log(ui.value)
});

or as a option on init

$("#age").spinner({
    min: 1,
    max: 99,
    spin: function( event, ui ) {
        console.log(ui.value)
    }
});

다른 팁

Use the 'stop' event instead of the 'spin' event.

For me, this worked, while using the 'spin' event gave me the same problem as the PO.

Quoting from the jQueryUI-API: (emphasis by me)

stop( event, ui ) Type: spinstop Triggered after a spin.

$( ".selector" ).spinner({
  stop: function( event, ui ) {}
});

Bind an event listener to the spinstop event:

$( ".selector" ).on( "spinstop", function( event, ui ) {} );

Source: http://api.jqueryui.com/spinner/#event-stop

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top