是否在屏幕上显示javascript验证消息而不是警报消息?
我用的是struts1.3.8。我正在使用struts ValidatorPlugIn生成客户端和服务器端验证消息。是否在屏幕上显示javascript验证消息而不是警报消息?,javascript,struts,struts-1,Javascript,Struts,Struts 1,我用的是struts1.3.8。我正在使用struts ValidatorPlugIn生成客户端和服务器端验证消息。 现在,客户端javascript由validator插件生成。如果存在任何验证错误,它将显示在警报消息中。但是我想在文本字段之外显示它们。 我现在仍然只处理警报消息。。但现在要求改变了。我试过了,但没有用…… 怎么做? 这是插件生成的代码 `enter code here` function jcv_handleErrors(messages, focusField) {
现在,客户端javascript由validator插件生成。如果存在任何验证错误,它将显示在警报消息中。但是我想在文本字段之外显示它们。
我现在仍然只处理警报消息。。但现在要求改变了。我试过了,但没有用……
怎么做?
这是插件生成的代码 `enter code here` function jcv_handleErrors(messages, focusField) { if (focusField && focusField != null) { var doFocus = true; if (focusField.disabled || focusField.type == 'hidden') { doFocus = false; } if (doFocus && focusField.style && focusField.style.visibility && focusField.style.visibility == 'hidden') { doFocus = false; } if (doFocus) { focusField.focus(); } } alert(messages.join('\n')); } `在此处输入代码`函数jcv_handleErrors(消息,焦点字段){ if(focusField&&focusField!=null){ var doFocus=true; if(focusField.disabled | | focusField.type=='hidden'){ doFocus=false; } if(doFocus&& focusField.style&& focusField.style.visibility&& focusField.style.visibility==“隐藏”){ doFocus=false; } if(doFocus){ focusField.focus(); } } 警报(messages.join('\n'));
} 在没有具体信息的情况下,我能真正建议的只是以下内容的变化:
window.alert = function(message){
console.log(message);
}
这只需确保传递到alert()
的任何消息都被传递到console.log()
相反,您可以将消息定向到特定元素:
window.alert = function(message) {
var output = document.getElementById('output'),
newTextContainer = document.createElement('p'),
text = document.createTextNode(message);
newTextContainer.appendChild(text);
output.appendChild(newTextContainer);
}
但是,使用这两种方法都会中断页面中alert()
函数的任何使用。因此,我建议使用后一个示例(就在上面)创建一个新函数并调用该函数,而不是过度编写alert()
关于创建自定义函数来处理警报,以及指定新“警报”应附加到的特定元素:
function newAlert(message, elem) {
// message is a string containing the message to display.
// elem is the id of the element into which the message should be displayed,
// defaults to an id of 'output' if no element is specified.
var output = elem ? document.getElementById(elem) : document.getElementById('output'),
newTextContainer = document.createElement('p'),
text = document.createTextNode(message);
newTextContainer.appendChild(text);
output.appendChild(newTextContainer);
}
针对OP提出的问题进行编辑,如下所示: 下一步再次提交我要覆盖上一条错误消息的表单。不会两次显示相同的消息 有两种方法可以做到这一点,假设您只想显示最后一条错误消息,而不是附加这些错误消息;在第一个示例中,我使用
while
循环删除output
元素的firstChild
,如果为空,则附加新的错误消息:
function newAlert(message, elem) {
var output = elem ? document.getElementById(elem) : document.getElementById('output'),
newTextContainer = document.createElement('p'),
text = document.createTextNode(message);
while (output.firstChild){
output.removeChild(output.firstChild);
}
newTextContainer.appendChild(text);
output.appendChild(newTextContainer);
}
另一种方法是获取对输出
元素中第一个段落元素的引用(如果存在,则创建一个),然后简单地覆盖该元素中的文本:
function newAlert(message, elem) {
var output = elem ? document.getElementById(elem) : document.getElementById('output'),
textContainer = output.getElementsByTagName('p')[0] || output.appendChild(document.createElement('p'));
if (textContainer.firstChild){
textContainer
.firstChild
.nodeValue == message;
}
else {
textContainer
.appendChild(document
.createTextNode(message));
}
}
你能告诉我们处理警报的代码部分吗?是的,你能。你的意思是只显示最后一条错误信息,而不是全部错误信息?我的意思是第一次不输入用户名和密码,显示错误信息。。下一步再次提交我要覆盖上一条错误消息的表单。。不会两次显示相同的消息。。