简体   繁体   中英

spin event doesn't trigger at first time in jquery spinner

I have created a custom spinner which appends a string after a number.

var percent = 5.5%;
$.widget("ui.pcntspinner", $.ui.spinner, {
        _format: function(value){
            return value + " %";
        },

        _parse: function(value){
            return parseInt(value);
        }
    });


    $("#spinner").pcntspinner().val(percent);

The spinner has two events attached to it ie focusout and spin. focusout works fine in first call too, but spin does not gets triggered at first time. Only after any other event has been triggered does the spin gets called. Any idea to solve this problem?

You are extending the widget so spin won't work you need _spin

$.widget("ui.pcntspinner", $.ui.spinner, {
    _format: function(value){
        return value + " %";
    },

    _parse: function(value){
        return parseInt(value);
    },

    _spin: function( event, ui ) { 
       alert('value updated by :' + ui);

       if(validation){
         var result = this._super( event, ui ); 
         return result; //<-- important for generic functionality
       }else
       {
         //returning nothing would make value not change
       }
    }
});

Found the solution. I still don't understand though why spin was not getting triggered the first time but this helped -

var percent = 5.5%;
$.widget("ui.pcntspinner", $.ui.spinner, {

         widgetEventPrefix: "spin",
        _format: function(value){
            return value + " %";
        },

        _parse: function(value){
            return parseInt(value);
        }
    });


    $("#spinner").pcntspinner().val(percent);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM