我正在尝试从javascript验证中加入错误消息,我只收到一条消息,并显示模式(每次只有一个消息):
)如何加入所有错误消息以一次显示所有错误消息?
function updateErrorMessage(msg) {
var errorMessage = msg;
var modal = document.querySelector('.modal-wrap');
document.getElementById('close').onclick = function() {
removeClass(modal, 'md-active');
}
document.getElementById('modalErrormessage').innerHTML = errorMessage;
addClass(modal, 'md-active');
}
编辑:我确实以这种方式捕获错误消息:
if (Protocol != '' && Protocol.length < 11) {
formatErrorMessage('wrong protocol');
updateErrorMessage('Please! correct protocol.');
e.preventDefault(e);
e.stopPropagation(e);
}
我尝试使用推送,但我确实会收到未定义的错误消息:
EDIT:
function formatErrorMessage(msg){
var errorMessage = msg;
var textMsg = [];
textMsg.push(errorMessage);
var finalErrorMessage;
mensagemErroFinal = textMsg.join(', ');
return mensagemErroFinal;
}
推动将添加到数组中,您需要使用.join将数组的元素连接到字符串中。https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/array/join/join
另外,您要收到该控制台日志的未定义错误的原因是,您正在使用PHP语法添加TextMSG。您必须使用 这样的使用:console.log('errorMessage ' + textMsg);