dongzhuzhou4504 2014-03-21 21:16
浏览 69
已采纳

如何通过javascript更改textContent?

I have a registration form page, and there is empty div i want to use to display errors. To check forms before triggering php script i use javascript:

function errorHandler(){
    var loginIn;
    var passIn;


    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;

    if (loginIn == "" || loginIn == null) {
        alert("LOGIN CANNOT BE EMPTY");
        return false;
    } 
}


It works fine, and alert message do appear when i call them like this:

<form name="regForm" action= "save_user.php" onsubmit="return errorHandler()" method="post">.

But there is as i mentioned before a div inside of the form:

div id ="errorArea"></div> and when i try to put a text inside of this div like this:

function errorHandler(){

    var loginIn;
    var passIn;
    var erorAreaMessage;

    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;
    erorAreaMessage = document.getElementById('errorArea').textContent;

    if (loginIn == "" || loginIn == null) {
        erorAreaMessage = "LOGIN CANNOT BE EMPTY";
        return false;
    }
}

Nothing happens, can someone explain me why?

  • 写回答

1条回答 默认 最新

  • dqm74406 2014-03-21 21:19
    关注

    You need to put the value inside the <div>. That can done by setting the innerHTML or textContent property to your error-message. Try this -

    ...
    if (loginIn == "" || loginIn == null) {
        document.getElementById('errorArea').textContent = "LOGIN CANNOT BE EMPTY";
        return false;
    }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?