尝试使用错误消息验证我的表单似乎不起作用

时间:2017-01-15 03:34:07

标签: javascript

我想传递具有相同功能的不同元素ID

public ICommand Maintain;
public ICommand Dispatch;
public ICommand Memorize;
//............//

private class ReckonCommand : ICommand {
    public float commandName { get; set; }
    //Stuff

}

public Commands () {
    System.Reflection.FieldInfo[] _fields = GetType ().GetFields ();
    for (int i = 0; i < _fields.Length; i++) {
        if (_fields [i].FieldType == typeof(ICommand)) {
            _fields [i].SetValue (this, new ReckonCommand () as ICommand);
        }
    }
}

我收到第二个var和我的html onClick的错误。我的问题不同,因为这里没有正确回答这个问题的答案。我尝试了一切。我不知道为什么我收到此错误消息。

2 个答案:

答案 0 :(得分:0)

如果您尝试在提交时验证所有元素,请点击。

您可以使用querySelector选择全部,然后检查每个元素的值。如果价值是&#34;&#34;然后改变兄弟span元素的innerHTML。

&#13;
&#13;
var submit = document.querySelector("#btn")

submit.addEventListener("click", function() {


  var elements = document.querySelectorAll("input");

  for (var i = 0; i < elements.length; i++) {
    if (elements[i].value === "") {

      elements[i].nextElementSibling.innerHTML = "Required Field";

    }
  }
});
&#13;
<body>

  <h1>SUBSCRIBE</h1>


  <form id="frml" method="get">

    <input type="text" name="fname" placeholder="First Name" class="textbox" id="firstname" /><span id="spnfirstname"></span>
    <br />

    <input type="text" name="lname" placeholder="Last Name" class="textbox" id="lastname" /><span id="spnlastname"></span>
    <br />

    <input type="date" name="Birthday" value="" class="textbox" id="birthday" />

    <span id="spnbirthday"></span>
    <br />

    <input type="email" name="" placeholder="someone@example.com" class="textbox" id="email" /><span id="spnemail"></span>
    <br />

    <input type="tel" name="" placeholder="Home Phone" class="textbox" id="homep" /><span id="spnhomep"></span>
    <br />

    <input type="tel" name="" placeholder="Cell Phone" class="textbox" id="cellp" /><span id="spncellp"></span>
    <br />

    <input id="btn" type="button" value="Submit" />

  </form>

  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

两件事:

1.-使用'value'属性从输入中获取实际值:

var getId = document.getElementById(idname).value;

2.将函数设置为'click'事件,如果要在表单后面使用普通的javaScript,addEventListener会很有用:

<script>
 document.getElementById('btn').addEventListener('click', function(){validate('firstname', 'spnfirstname');});
</script>

这就是我的工作方式。

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <script>
        function validate(idname, spnname){
           var getId = document.getElementById(idname).value;
            console.log(getId);
            if(getId === null || getId === undefined || getId === ""){
                var getSpn = document.getElementById(spnname).innerHTML = "Required Field";
            }
        }
    </script>
    </head>
    <body>
        <h1>SUBSCRIBE</h1>
        <form id="frml" method="get">
            <input type="text" name="fname" placeholder="First Name" class="textbox" id="firstname"/><span id="spnfirstname"></span><br/>
            <input type="text" name="lname" placeholder="Last Name" class="textbox" id="lastname"/><span id="spnlastname"></span><br/>
            <input type="date" name="Birthday" value="" class="textbox" id="birthday"/>
            <span id="spnbirthday"></span><br/>
            <input type="email" name="" placeholder="someone@example.com" class="textbox" id="email"/><span id="spnemail"></span><br/>
            <input type="tel" name="" placeholder="Home Phone" class="textbox" id="homep"/><span id="spnhomep"></span><br/>
            <input type="tel" name="" placeholder="Cell Phone" class="textbox" id="cellp"/><span id="spncellp"></span><br/>
            //is there another way to do this onClick event in javascript that may help
            <input id ="btn" type="button" value="Submit"/>
        </form>
        <script>
            document.getElementById('btn').addEventListener('click', function(){validate('firstname', 'spnfirstname');});
        </script>
    </body
</html>