English 中文(简体)
利用j Query/javascript限制价格现场投入
原标题:using jQuery/javascript to restrict price field input

我在互联网上发现了以下 j,但我很快发现,这部法律存在缺陷,因为它不接受一个小点(第46条),尽管该法典似乎允许这样做。

目前,我不能像1.23一样进入价格,因为这一时期被忽略,而我却获得123。

没有人会担心这种情况没有奏效?

// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;

            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

我正在使用这样的花 the:

$(function(){
  $( #price_field ).ForceNumericOnly();
});
最佳回答

Just add the . (code 190 and 110) to the checks:

// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;

            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 190 ||   // normal .
                key == 110 ||   // keypad .
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};
问题回答

其他用户已经回答了你的问题,但我要向你提供这一联系。

rel=“nofollow”http://unixpapa.com/js/key.html

我认为,在处理关键事件并使其相互兼容时非常有用。

我希望这一帮助。

您应加上第190号关键法典,接受“......”。

// Numeric only control handler
$.fn.ForceNumericOnly = function() {
return this.each(function()
{
    $(this).keydown(function(e)
    {
        var key = e.charCode || e.keyCode || 0;

        // allow dot, backspace, tab, delete, arrows, numbers and keypad numbers ONLY
        return (
            key == 190 ||  
            key == 8 ||
            key == 9 ||
            key == 46 ||
            (key >= 37 && key <= 40) ||
            (key >= 48 && key <= 57) ||
            (key >= 96 && key <= 105));
    })
})
};

You need to include 190 and 110 for both decimal points.

// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;

            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 190 ||    //add this line. 190 is the keycode for a period
                key == 110 ||    //and this line. 110 is the keycode for a decimal
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签