JavaScript检查&在提交按钮单击时动态地向表中添加文本

时间:2012-11-16 16:11:35

标签: javascript appendchild createtextnode

基本上我的HTML看起来像这样:

<form method="post" name="htmlform" onsubmit = "checkFields()">
  <table style="width: 479px;" border="30" cellspacing="3" cellpadding="10">
  <tbody>
  <tr>
  <td valign="top"><span id="firstNameSpan" >First Name *</span></td>
  <td valign="top"><input type="text" name="first_name" id="first_name"
                    size="30" maxlength="50" /></td>
  </tr>
  <tr>
  <td valign="top"><span id = "lastNameSpan" >Last Name *</span></td>
  <td valign="top"><input type="text" name="last_name" size="30" maxlength="50"/> 
      /td>
  </tr>
  <tr>
  <td style="text-align: center;" colspan="2"><input type="radio" name="sex" 
  value="male" /> Male <input type="radio" name="sex" 
  value="female" /> Female</td>
  </tr>
  <tr>
  <td style="text-align: center;" colspan="2"><input type="submit" value="submit" 
       /></td>
  </tr>
  </tbody>
  </table>
  </form>

提交表单时,onsubmit()事件检查first_name文本字段是否为空,如果是,则左边的标签或跨度将附加到输出“first name *”+“你必须输入名字”同样的姓氏和性别。

问题是表中的文本没有用appendchild更新。当我将语句包含在警报中进行调试时,会附加消息,然后消失。

以下是onsubmit =“checkFields()”的JavaScript代码。

function checkFields() {
    var firstName = document.getElementById("first_name").value;
    var lastName = document.getElementById("last_name").value;
    if (firstName == "") {
        //<span style='color:red'> Please enter a first name </span>
        var nameHint = " Please enter a first name";
        var node = document.getElementById("firstNameSpan");
        //alert(node.appendChild(document.createTextNode(nameHint)) );
        //not working
        node.appendChild(document.createTextNode(nameHint));

    } if (lastName == "") {
        //additional code
    }
}

提前致谢,非常感谢您的帮助。还有任何JavaScript调试器吗?

问候

David D

2 个答案:

答案 0 :(得分:0)

我认为由于html不正确,您的示例代码无效。 document.getElementById("last_name")将返回undefined。

http://jsfiddle.net/5E6my/1/

答案 1 :(得分:0)

非常感谢,尽管错误消息对调试没有用,但是我在分屏时获得了JFiddle的绳索是非常有用的。这是完成的代码(1)HTML(2)JavaScript。

<form method="post" name="htmlform" onsubmit="return checkFields();">
  <table style="width: 479px;" border="30" cellspacing="3" cellpadding="10">
  <tbody>
  <tr>
  <td valign="top"><span id="firstNameSpan" >First Name *</span></td>
  <td valign="top"><input type="text" name="first_name" id="first_name"
                                            size="30" maxlength="50" /></td>
  </tr>
  <tr>
  <td valign="top"><span id = "lastNameSpan" >Last Name *</span></td>
  <td valign="top"><input type="text" name="last_name" id="last_name" size="30" 
  maxlength="50" /></td>
  </tr>
  <tr>
  <td style="text-align: center;" colspan="2" id = "sexMessage">
      Please select Male or Female*</td>
  </tr>
  <tr>
  <td style="text-align: center;" colspan="2"><input type="radio"
   name="sex" value="male" /> Male <input type="radio" name="sex"
  value="female" /> Female</td>
  </tr>
  <tr>
  <td style="text-align: center;" colspan="2"><input type="submit" value="submit"    
   /></td>
  </tr>
  </tbody>
  </table>
  </form>  

对表单中的onsubmit按钮的未填充字段作出反应的JavaScript代码是:(任何使这段代码更简单的方法吗?)

window.checkFields = function()
  {
  var firstName = document.getElementById("first_name");
  var lastName = document.getElementById("last_name");
  if(firstName.value == "")
  {
  //<span style='color:red'> Please enter a first name </span>
    var nameHint = " *Please enter a first name ";
    var fnNode = document.getElementById("firstNameSpan");
    while(fnNode.firstChild)
    {
        fnNode.removeChild(fnNode.firstChild)
    }
    fnNode.appendChild(document.createTextNode(nameHint));
    fnNode.style.color="red";

  } else{
      var nameHint = " First Name *";
      var fnNode = document.getElementById("firstNameSpan");
      while(fnNode.firstChild)
      {
        fnNode.removeChild(fnNode.firstChild)
      }
      fnNode.appendChild(document.createTextNode(nameHint));
      fnNode.style.color="black";
  }
  if (lastName.value == "")
  {
  //additional code
    var nameHint = " *Please enter a last name";
    var lnNode = document.getElementById("lastNameSpan");
    while(lnNode.firstChild)
    {
        lnNode.removeChild(lnNode.firstChild)
    }
    lnNode.appendChild(document.createTextNode(nameHint));
    lnNode.style.color="red";
  } else{
      var nameHint = " Last Name *";
      var lnNode = document.getElementById("lastNameSpan");
      while(lnNode.firstChild)
      {
        lnNode.removeChild(lnNode.firstChild)
      }
      lnNode.appendChild(document.createTextNode(nameHint));
      lnNode.style.color="black";
  }
      var radios = document.getElementsByName("sex");
      var radioValue = ""
      for(var i=0; i<radios.length; i++)
      {
          if(radios[i].checked)
          {
              radioValue = radios[i].value;
          }

      }
      if(radioValue === "")
      {
          var sexNode = document.getElementById("sexMessage");
          var nameHint = "*You did not choose a sex";
          while(sexNode.firstChild)
          {
              sexNode.removeChild(sexNode.firstChild);
          }
          sexNode.appendChild(document.createTextNode(nameHint));
          sexNode.style.color="red";
      } else {
          var sexNode = document.getElementById("sexMessage");
          var nameHint = "Please select Male or Female*";
          while(sexNode.firstChild)
          {
              sexNode.removeChild(sexNode.firstChild);
          }
          sexNode.appendChild(document.createTextNode(nameHint));
          sexNode.style.color="black";
      }               
      return false;
  }

诀窍是使用Yury建议的是onsubmit =“return checkfields()”然后在代码块中使用window.checkFields = function(){etc。

我有一个问题...... JQuery使用起来要简单得多,我在JQuery之前学习JavaScript ......我应该跳过JQUery。 JQuery也支持AJAX框架吗?

非常感谢

大卫