Javascript 仅允许基于连续id的文本框中的数字
这是只允许输入数字的 我想根据id在中执行此操作 所以我创造了这个Javascript 仅允许基于连续id的文本框中的数字,javascript,php,jquery,html,Javascript,Php,Jquery,Html,这是只允许输入数字的 我想根据id在中执行此操作 所以我创造了这个 $(文档).ready(函数(){ //在文本框中按下键时调用 $(“#数量”)。按键(功能(e){ //如果字母不是数字,则显示错误,不键入任何内容 如果(e.which!=8&&e.which!=0&&e.which57)){ //显示错误消息 $(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”); 返回false; } }); }); 我怎么能对像quantity1,quantity
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“#数量”)。按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
我怎么能对像quantity1,quantity2这样的一系列id这样做
更新:
我不能用类名。。仅凭id
试试这个
HTML
编号:
第一:
第一:
Jquery
$(document).ready(function () {
for(var i = 1; i< 4 ;i++)
{
//called when key is pressed in textbox
$("#quantity"+i).keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
}
});
$(文档).ready(函数(){
对于(变量i=1;i<4;i++)
{
//在文本框中按下键时调用
$(#数量+i)。按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
}
});
您所要做的就是匹配所有简单的id。看这个
这是零件代码-
$(document).ready(function () {
//called when key is pressed in textbox
$("[id^=quantity]").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“[id^=quantity]”。按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
使用类名-$(.quantity)
我已经更新了我的问题。。i、 例如,仅使用id
而不使用类名
$(document).ready(function () {
for(var i = 1; i< 4 ;i++)
{
//called when key is pressed in textbox
$("#quantity"+i).keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
}
});
$(document).ready(function () {
//called when key is pressed in textbox
$("[id^=quantity]").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});