Javascript Jquery错误消息显示在每个字段上
我有一个html表单,在一些字段中只有数字需要提交。如果输入文本,它将显示错误消息。它没有达到标准。在三个字段中,如果有人输入了任何文本,则在所有三个字段中都会显示错误消息 我想要的是-显示当前字段而不是所有字段的错误消息 Jquery代码是:Javascript Jquery错误消息显示在每个字段上,javascript,jquery,html,forms,Javascript,Jquery,Html,Forms,我有一个html表单,在一些字段中只有数字需要提交。如果输入文本,它将显示错误消息。它没有达到标准。在三个字段中,如果有人输入了任何文本,则在所有三个字段中都会显示错误消息 我想要的是-显示当前字段而不是所有字段的错误消息 Jquery代码是: $(document).ready(function () { //called when key is pressed in textbox $(".quantity").keypress(function (e) { //if th
$(document).ready(function () {
//called when key is pressed in textbox
$(".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(函数(){
//在文本框中按下键时调用
$(“.quantity”).按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“.errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
HTML代码是:
<form method="post" name="data" action=" ">
Amount: <span class="errmsg"></span><br>
<input type="text" name="amount" class="quantity" /><br><br>
Interest: <span class="errmsg"></span><br>
<input type="text" name="interest" class="quantity" /><br><br>
Duration: <span class="errmsg"></span><br>
<input type="text" name="duration" class="quantity" /><br><br>
<input type="submit" name='submit' onclick="show_confirm()" value="SUBMIT">
</form>
金额:
兴趣:
持续时间:
$(文档).就绪(函数(){
//在文本框中按下键时调用
$(“.quantity”).按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(this).prevAll(“.errmsg”).first().html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
$(文档).就绪(函数(){
//在文本框中按下键时调用
$(“.quantity”).按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(this).prevAll(“.errmsg”).first().html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
以下是我的方法:
$(函数(){
$(“.quantity”).按键(功能(e){
var name=$(this.prop('name');
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
$(.errmsg#err-“+name).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
函数show_confirm(){}
金额:
兴趣:
持续时间:
以下是我的方法:
$(函数(){
$(“.quantity”).按键(功能(e){
var name=$(this.prop('name');
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
$(.errmsg#err-“+name).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
函数show_confirm(){}
金额:
兴趣:
持续时间:
您可以使用.prev()
在输入之前选择错误消息span
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“.quantity”).按键(功能(e){
var elem=$(本);
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
elem.prev().prev(“.errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});代码>
HTML代码是:
金额:
兴趣:
持续时间:
您可以使用.prev()
在输入之前选择错误消息span
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“.quantity”).按键(功能(e){
var elem=$(本);
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
elem.prev().prev(“.errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});代码>
HTML代码是:
金额:
兴趣:
持续时间:
$(document).ready(function () {
//called when key is pressed in textbox
$(".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
$(this).prevAll(".errmsg").first().html("Digits Only").show().fadeOut("slow");
return false;
}
});
});