如何在使用Javascript输入旁边显示所需的消息? [英] How to show a required message next to input with Javascript?
本文介绍了如何在使用Javascript输入旁边显示所需的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让字段是必需的,但我希望错误显示在字段旁边,它本身不会被警告?
I'm trying to make fields required but I want the error to show up next to the field it self not be alert?
推荐答案
在输入标记旁边添加span标记
Add span tag next to input tag
<span id="first-name-err"></span>
并改变你的javascript代码如下
and change you javascript code as below
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
}
var flag = true;
for (var i = 0; i < rules.length; i++) {
alert(rules[i][0]);
if (!rules[i][1]) {
var parent = elem(rules[i][0]).parentNode,
message = 'Please check your ' + (parent.textContent || parent.innerText).replace(/^\s*(.*?)\s*$/, '$1').slice(0, -1).toLowerCase() + '.';
var id = rules[i][0]+'-err';
document.getElementById(rules[i][0]+'-err').innerHTML = message;
flag = false;
}
}
return flag;
};
检查这
这篇关于如何在使用Javascript输入旁边显示所需的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文