简体   繁体   中英

jQuery Knob format causing scroll issue

I'm using the jQuery Knob plugin but need the displayed value to be in pound sterling. Ideally this will look like £123456. However, when I use the 'format' hook to add the £ sign it no longer allows me to set the value by scrolling the mousewheel or even by typing it in. Here's my code...

$(".dial-step1").knob(
{
   'format': function( value ){
       if(value == undefined || isNaN(value)) {
         value = 0; 
         return '£' + value;
       }
       else {
         return '£' + value;
       }
    }
}
);

It works if I change the position of the £ sign to be after the value - return value + '£'; - but I really need it before. Any idea why this would be breaking the scroll and keyboard input functionality?

I know there are a bunch of similar questions to this but all of them seem to be for adding a unit to the end of the value...

Simply use the corresponding parse option to define your value parsing method:

$(".dial-step1").knob({
    format: function( value ){
        if (value == undefined || isNaN(value)) {
            value = 0; 
        }
        return '£' + value;
    },
    parse: function( value ){
        var v = value.toString();

        if (v.substring(0, 1) == '£') { 
            v = v.substring(1);
        }

        return parseFloat(v);
    }
});

Full example: JSFiddle

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