Javascript将警报转换为字符串

时间:2014-12-04 17:01:22

标签: javascript string alert

我有这个小javascript来验证表单输入。

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.courriel);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

我试着弄明白如何获得警告"消息作为一个字符串,以便稍后在页面上显示它,而不是弹出这个荒谬的框。对我来说看起来很简单,但我在js中是一个很大的零。任何帮助表示感谢,谢谢!

3 个答案:

答案 0 :(得分:1)

alert()方法是大弹出框的原因。如果要保存该值以供以后使用,请将其保存到变量中。类似的东西:

var message = "";
function validateFormOnSubmit(theForm) {
   var reason = "";

   reason += validateEmail(theForm.courriel);

   if (reason != "") {
      message = "Some fields need correction:\n" + reason;
      return false;
   }

   return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

答案 1 :(得分:0)



var alerter;

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.courriel);

  if (reason != "") {
    alerter = "Some fields need correction:<br/>" + reason;
    return false;
  }

  return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

document.body.innerHTML = alerter;
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如何将字符串分配给&#34; window&#34;的属性? :

window.myAlert ="Some fields need correction:\n" + reason);

然后你可以从任何地方获得它:

document.body.innerHTML = window.myAlert;