繁体   English   中英

从javascript打印或回显html页面中的消息

[英]Printing or Echoing message in html page from javascript

我有以下脚本

function validateEmailp() {
var messagemail = document.getElementById('emailerrorz').innerText;
var two = document.getElementById('email').value;
var first = two.split("@")[1];
var badEmails = ["gmail.com", "yahoo.com"]
if (badEmails.indexOf(first) != -1) {
        document.getElementById("email").value = ""; //this works
    messagemail = 'We do not accept free e-mails'; //this doesn't
    return false;
    }   
return true;
}

和HTML

<td>{EMAILFIELD}<span id="emailerrorz"></span></td>

并且{EMAILFIELD}在PHP中

<input id="email" class="txt" type="text" name="email" size="25" value="" maxlength="255" onblur="validateEmailp()"></input>

但这对我无法显示范围ID中的错误。 它仅适用于从那里重置值。

属性无法以这种方式工作。 你要:

 document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails'

要么

  var messagemail = document.getElementById('emailerrorz');
  ....
  messagemail.innerText = etc

http://jsfiddle.net/MJXEg/

当您执行var messagemail = document.getElementById('emailerrorz').innerText; 您的变量存储带有该内容的字符串。

当您var messagemail = document.getElementById('emailerrorz'); 您的变量存储了一个对象/元素,然后可以使用属性.innerText

因此使用:

var messagemail = document.getElementById('emailerrorz');
// rest of code
messagemail.innerText = 'We do not accept free e-mails';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM