【发布时间】:2016-01-03 04:30:40
【问题描述】:
我正在尝试定位页面上的所有输入 type="number",但只有第一个被触发。有谁知道我为什么以及如何定位所有输入元素?
我的 JS:
// Select your input element.
var numInput = document.querySelector('input');
// Listen for input event on numInput.
numInput.addEventListener('input', function(){
// Let's match only digits.
var num = this.value.match(/^\d+$/);
if (num === null) {
// If we have no match, value will be empty.
this.value = "";
}
}, false)
【问题讨论】:
-
你为什么不试试jQuery?它的语法很短 $('input[type=number]')
标签: javascript jquery