简体   繁体   English

JQuery类选择器

[英]JQuery class selectors

I have an html text box with some having class names as numbers 我有一个html文本框,其中一些有类名作为数字

<input type="text" name="msg_timeout" class="numbers" />

similarly there are different text box with class as numbers .I want to assign keydown event to those text box that has class as number so i tried with following ,but not working 类似地,有不同的文本框,类作为数字 。我想将keydown事件分配给那些具有类作为数字的文本框,所以我尝试使用以下,但不工作

$('input.numbers').each

$('.numbers').each

$('input.numbers:text').each

$('input:text.numbers').each

$('input[type=text]').each  // only this is working but it selects all textboxes.

kindly let me know idea. 请让我知道。 CODE BELOW 代码如下

$(document).ready(function() {


    $('input.numbers').each(function() {

        $(this).get(0).oncontextmenu = function() { return false; };
           $(this).bind("keydown",function(event) {
           // alert(window.event);

            // Allow only backspace and delete
            if ( event.keyCode == 46 || event.keyCode == 8  
                    && (event.keyCode >=96 && event.keyCode <=105) ) 
            {
                // let it happen, don't do anything
            }
            else {
                // Ensure that it is a number and stop the keypress
                if (event.keyCode < 48 || event.keyCode > 57 || event.shiftKey || event.ctrlKey || event.altKey ) {
                    event.preventDefault(); 
                }   
            }

            var forbiddenKeys = new Array('c', 'x', 'v');
            var keyCode = (event.keyCode) ? event.keyCode : event.which;
            var isCtrl;
            isCtrl = event.ctrlKey;
            if (isCtrl) {
                for (i = 0; i < forbiddenKeys.length; i++) {
                    if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                        //alert('You are prompted to type this twice for a reason!');
                        return false;
                    }
                }
            }
            return true;
        });
   });

});

Are you calling the selectors after dom.ready? 你是否在dom.ready之后调用选择器?

$(document).ready(function() {
    $('input.numbers').keydown(function() {
        // code here
    });
});

without the $(document).ready() it's unpredictable which elements will be present on the screen when your selector is evaluated. 如果没有$(document).ready()那么在评估选择器时,屏幕上会出现哪些元素是不可预测的。

You don't need to explicitly iterate over all matching elements to assign event handlers to them. 您不需要显式迭代所有匹配元素来为它们分配事件处理程序。 The following will do the trick: 以下将做到这一点:

// bind a keydown handler to all input elements with class 'numbers'
$("input.numbers").keydown(function() {
    // handler implementation here
});

You haven't posted your entire code, but I'm guessing you might also be adding some elements on the fly? 你没有发布你的整个代码,但我猜你也可能会动态添加一些元素?

In that case.. use.. 在那种情况下..使用..

$("input.numbers").live('keydown', function() {
    // do stuff here
});

you mean "numbers" not as text but as a number, a digit, integer? 你的意思是“数字”不是文本而是数字,数字,整数?

$("input").filter(function() {
   return $(this).attr("class").match(/\d+/);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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