jQuery UI Spinner with letters A-Z or other custom range

北城以北 提交于 2019-12-04 02:06:19

问题


Is there a way to customize jQuery UI spinner, so that A-Z letters (or any custom range) is possible?


回答1:


Yes, this is possible. Here's a simple example using A-Z, adapted from the provided time example:

$.widget("ui.alphaspinner", $.ui.spinner, {
    options: {
        min: 65,
        max: 90
    },
    _parse: function(value) {
        if (typeof value === "string") {
            return value.charCodeAt(0);
        }
        return value;
    },
    _format: function(value) {
        return String.fromCharCode(value);
    }
});

Usage:

$("#my-input").alphaspinner();

Example: http://jsfiddle.net/4nwTc/1/

The above example creates a new widget called alphaspinner that inherits from spinner. You can do this for just one spinner with the following:

$(function() {
    var spinner = $("#alpha-spinner").spinner({
        min: 65,
        max: 90
    }).data("spinner");

    spinner._parse = function (value) {
        if (typeof value === "string") {
            return value.charCodeAt(0);
        }
        return value;        
    };

    spinner._format = function (value) {
        return String.fromCharCode(value);        
    }
});​

Example: http://jsfiddle.net/4nwTc/2/




回答2:


I built up on Andrews code and built a spinner widget that takes a string array for input.

You can see the solution here.



来源:https://stackoverflow.com/questions/13496943/jquery-ui-spinner-with-letters-a-z-or-other-custom-range

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