【问题标题】:A Switch Within A Javascript FunctionJavascript 函数中的开关
【发布时间】:2013-06-13 13:52:55
【问题描述】:

我正在编写联系表格。

我的头部有两个 Javascript 函数(稍后会移动它们,但这是另一个问题)。

onblur 事件的第一个函数Validate() 不起作用。 用于 onsubmit 事件的第二个函数 formValidate() 有效。

我想在 javascript 函数中使用 switch 语句以在 html onblur 事件中使用。

Javascript:

<head>
<script type="text/javascript">
function Validate()
        {
            // create array containing textbox elements
            var input = [document.getElementById('fname'), document.getElementById('lname'), document.getElementById('email1'), document.getElementById('email12'), document.getElementById('message')];

               for(var i = 0; i<input.length; i++)
            // loop through each element to see if value is empty
            {
                if(input[i].value == '')
                {
                        switch (ID){

                        case 'fname':
                        alert ('enter you first name');
                        break;
                        case 'lname' :
                        alert ('enter your last name');
                        break;
                        case 'email1':
                        alert ('enter your email address');
                        break;
                        case 'email2':
                        alert ('enter your email address');
                        break;
                        case 'message':
                        alert ('write your message');
                        break;                          
                    }                   
                }                   
            }            
         }

function formValidate()
        {
            // create array containing textbox elements
            var inputs = [document.getElementById('fname'), document.getElementById('lname'), document.getElementById('email1'), document.getElementById('email2'), document.getElementById('message')];

            var error;

            for(var i = 0; i<inputs.length; i++)
            // loop through each element to see if value is empty
            {
                if(inputs[i].value == '')
                {
                    error = 'Please complete all fields.';
                    alert(error);
                    return false;
                    }
            }
         }
</script>
</head>

html:

<form onsubmit="return formValidate()"  action="mailto:admin@sfasc.com.au" method="post"  id="contactForm" >
        <fieldset>
          <dl>
            <dt> First Name:</dt>
            <dd>
              <input class="input" type="text"  name="fname" id="fname" onblur="Validate()" />
            </dd>
            <dt> Last Name:</dt>
            <dd>
              <input class="input" type="text"  name="lname" id="lname" onblur="Validate()"/>
            </dd>
            <dt> Email Address:</dt>
            <dd>
              <input class="input" type="text"  name="email1"  id="email1" onblur="Validate()"/>
            </dd>
            <dt> Email Address:</dt>
            <dd>
              <input class="input" type="text"  name="email2"  id="email2" onblur="Validate()"/>
            </dd>
            <dt> Message:</dt>
            <dd>
              <textarea  name="address" id="address" rows="10" cols="10" onblur="Validate()"></textarea>
            </dd>
            <dd>
              <input type="submit" value="submit" name="submit"  />
            </dd>
          </dl>
        </fieldset>
      </form>

我已经尝试删除第二个函数,但没有任何区别。 我只是在学习。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript html function switch-statement onblur


    【解决方案1】:

    这行不通,ID 没有在任何地方定义:

    switch (ID) {
    

    你应该用这个替换它:

    switch(input[i].id) {
    

    你应该做一些调试,错误肯定会出现在控制台!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2013-10-03
      • 1970-01-01
      • 2013-04-18
      • 2020-08-02
      相关资源
      最近更新 更多