Html 通过jQuery选择数字类型的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4887085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:29:56  来源:igfitidea点击:

Select inputs with number type through jQuery

jquery-selectorstypesnumbershtmlhtml-input

提问by Diogo Cardoso

How can I select all inputs with number type in jQuery?

如何在 jQuery 中选择所有带有数字类型的输入?

The following code doesn't work:

以下代码不起作用:

$(':input[type="number"]').click(function () { 
   alert('hello'); 
});

Thanks.

谢谢。

回答by user113716

Your selector is correct. Using the attribute-equals-selector(docs), it will select input elements with that type.

您的选择器是正确的。使用attribute-equals-selector(docs),它将选择具有该类型的输入元素。

You'll need to be sure the DOM is loaded before running it.

在运行之前,您需要确保 DOM 已加载。

Example:http://jsfiddle.net/H2aQr/

示例:http : //jsfiddle.net/H2aQr/

$(function() {
    $(':input[type="number"]').click(function () { alert('hello'); });
});

回答by EvilAmarant7x

You don't need the colon at the beginning, that's for selecting types of input, like $('input:text')

开头不需要冒号,这是用于选择输入类型,例如 $('input:text')

so $('input[type="number"]').click(function () { alert('hello'); });

所以 $('input[type="number"]').click(function () { alert('hello'); });

works just fine

工作得很好

回答by Fender

i think your problem is the ":" before input.

我认为您的问题是输入前的“:”。

try:

尝试:

$('input[type="number"]').click(function () { alert('hello'); }); 

回答by gearsdigital

As you can see here your code works well... http://jsfiddle.net/U47Vj/Maybe your Browser does not support this HTML5 Feature. In Chrome everything is fine. Same code in FF 3.6 breaks.

正如您在此处看到的,您的代码运行良好... http://jsfiddle.net/U47Vj/也许您的浏览器不支持此HTML5 功能。在 Chrome 中一切都很好。FF 3.6 中的相同代码中断。