2

基本上我的 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 文本字段是否为空,如果是,则将其左侧的标签或跨度附加到输出“名字*”+“您必须输入名字”,类似地姓氏和性别。

问题是表中的文本没有用 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 调试器吗?

问候

大卫 D

4

2 回答 2

0

我相信您的示例代码由于 html 不正确而无法正常工作。document.getElementById("last_name")将返回未定义。

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

于 2012-11-16T16:55:21.860 回答
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() { 等。

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

非常感激

大卫

于 2012-11-16T20:14:43.730 回答