1

次のスクリプトがあります

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にエラーを出力してもうまくいきません。そこから値をリセットする場合にのみ機能します。

4

2 に答える 2

1

プロパティはこのようには機能しません。あなたがしたい:

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

また

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

http://jsfiddle.net/MJXEg/

于 2013-10-04T08:50:20.090 に答える
1

変数を実行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';
于 2013-10-04T08:52:37.820 に答える