Jquery 需要在下一个最近的类中显示错误消息,具体取决于输入
Jquery need to show error message in the next nearest class, depending on input
这是示例 http://jsfiddle.net/y53cd3qd/
具有多个输入表单。在每个输入表单旁边都有span
class="numeric"
.
<input type="text" id="miles_per_gallon" class="numeric" >
<span class="errmsg" style="color:red"></span>
<br/>
<input type="text" id="mileage" >
<span class="errmsg" style="color:red"></span>
<br/>
没有父div 或其他元素
要。如果用户键入不是数字,则在最近或最近的 nex class="errmsg"
中显示错误
试
$(".numeric").keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//$(".errmsg").html("Digits Only").show().fadeOut("slow");
$(this).siblings(".errmsg").html("hmm");
//alert("Digits only");
return false;
}
});
但是在所有class="errmsg"
中显示错误消息。如何只在下一个最接近的地方显示?
使用这个:
$(this).next(".errmsg").html("hmm");
$(".numeric").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");
$(this).next(".errmsg").html("hmm");
//alert("Digits only");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="span_left">Miles per gallon<span class="required">*</span>: </span>
<input style="width:145px;" type="text" name="miles_per_gallon" id="miles_per_gallon" class="numeric" placeholder="Miles per gallon" value="" >
<span class="errmsg" style="color:red"></span>
<br/>
<span class="span_left">Mileage<span class="required">*</span>: </span>
<input style="width:145px;" type="text" name="mileage" id="mileage" value="" placeholder="Mileage" ><span class="errmsg" style="color:red"></span>
<br/>
我只是更改如下,
$(this).siblings(".errmsg").html("hmm");
to
$(this).next(".errmsg").html("hmm");
看看这个。
相关文章:
- jQuery在输入下验证post错误消息
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 解析云代码错误:解析.错误{代码:141,消息:“未调用成功/错误”}
- 在AngularJs中隐藏默认错误消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 使用ASP.NET CustomValidator避免重复的错误消息
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 更改精细上载中的错误消息
- 如何在页面刷新时隐藏错误消息
- 如何显示错误消息
- 滑块未显示,控制台中没有错误消息
- 表单提交没有'如果为空,则不会显示错误消息
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- 如何在消息中抛出tv4错误
- 登录时显示自定义错误消息
- jQuery AJAX 调用以发布消息 - 错误 堆栈空间不足
- 不断收到'向云PubSub发送测试消息错误…'Google Cloud PubSub
- 抑制WebBrowser JavaScript消息错误
- 在Chrome中解析SVG路径日志消息错误