2014-03-25 89 views
0

我有一些代碼,我一直有一些問題。出於某種原因,它會在檢查文本字段之前檢查單選按鈕是否被選中。但是,我需要它是相反的。我是新來的Javascript,所以任何幫助,將不勝感激。這裏是我的代碼:Javascript表單驗證 - 錯誤的訂單

<html> 
<head> 
    <title>Exam entry</title> 
    <script language="javascript" type="text/javascript"> 
    function validateForm(){ 
    var result = true; 
    var msg=""; 
    if (document.ExamEntry.name.value==""){ 
     msg+="You must enter your name \n"; 
     document.ExamEntry.name.focus(); 
     document.getElementById('name').style.color="red"; 
     result = false; 
    } 
    if (document.ExamEntry.subject.value==""){ 
     msg+="You must enter the subject \n"; 
     document.ExamEntry.subject.focus(); 
     document.getElementById('subject').style.color="red"; 
     result = false; 
    } 
    if (document.ExamEntry.number.value.length!="4") { 
     msg+="You must enter your exam number and it must be 4 digits long \n"; 
     document.ExamEntry.number.focus(); 
     document.getElementById('number').style.color="red"; 
     result = false; 
    } 
    var checked = null; 
    var inputs = document.getElementsByName('examtype'); 
    for (var i = 0; i < inputs.length; i++) { 
      if (inputs[i].checked) { 
      checked = inputs[i]; 
      break; 
     } 
    } 
    if(checked==null) 
    { 
     alert('Please choose an option'); 
     return false; 
    } 
    else{ 
     return confirm('You have chosen '+checked.value+' is this correct?'); 
    } 
    if(msg==""){ 
     return result; 
    } 
    else { 
     alert(msg); 
     return result; 
    } 
    } 
    </script> 
</head> 

<body> 
    <h1>Exam Entry Form</h1> 
    <form name="ExamEntry" method="post" action="success.html"> 
    <table width="50%" border="0"> 
    <tr> 
    <td id="name">Name</td> 
    <td><input type="text" name="name" /></td> 
    </tr> 
    <tr> 
    <td id="subject">Subject</td> 
    <td><input type="text" name="subject" /></td> 
    </tr> 
    <tr> 
    <td id="number">Exam Number</td> 
    <td><input type="text" name="number" /></td> 
    </tr> 
    <tr> 
    <td><input type="radio" id="examtype" name="examtype" value="GCSE">GCSE<br></td> 
    <td><input type="radio" id="examtype" name="examtype" value="AS">AS<br></td> 
    <td><input type="radio" id="examtype" name="examtype" value="A2">A2</td> 
    </tr> 
    <tr> 
    <td><input type="submit" name="Submit" value="Submit" 
      onclick="return validateForm();" /></td> 
    <td><input type="reset" name="Reset" value="Reset" /></td> 
    </tr> 
    </table> 
    </form> 
</body> 
</html> 

如果我沒有這段代碼

var checked = null; 
     var inputs = document.getElementsByName('examtype'); 
     for (var i = 0; i < inputs.length; i++) { 
       if (inputs[i].checked) { 
       checked = inputs[i]; 
       break; 
      } 
     } 
     if(checked==null) 
     { 
      alert('Please choose an option'); 
      return false; 
     } 
     else{ 
      return confirm('You have chosen '+checked.value+' is this correct?'); 
     } 

然後正常工作。

任何幫助是很大的:)

回答

1

使用此

function validateForm(){ 
    var result = true; 
    var msg=""; 
    if (document.ExamEntry.name.value==""){ 
     msg+="You must enter your name \n"; 
     document.ExamEntry.name.focus(); 
     document.getElementById('name').style.color="red"; 
     result = false; 
    } 
    if (document.ExamEntry.subject.value==""){ 
     msg+="You must enter the subject \n"; 
     document.ExamEntry.subject.focus(); 
     document.getElementById('subject').style.color="red"; 
     result = false; 
    } 
    if (document.ExamEntry.number.value.length!="4") { 
     msg+="You must enter your exam number and it must be 4 digits long \n"; 
     document.ExamEntry.number.focus(); 
     document.getElementById('number').style.color="red"; 
     result = false; 
    } 
    var checked = null; 
    var inputs = document.getElementsByName('examtype'); 
    for (var i = 0; i < inputs.length; i++) { 
      if (inputs[i].checked) { 
      checked = inputs[i]; 
      break; 
     } 
    } 
    if(checked==null) 
    { 
     msg+='Please choose an option'; 
     result= false; 
    }  
    if(msg==""){ 
     return confirm('You have chosen '+checked.value+' is this correct?'); 
    } 
    else { 
     alert(msg); 
     return result; 
    } 
    } 

DEMO

Updated Demo

+0

如果我只選擇1個單選按鈕,然後按確定,即使我沒有在文本字段中輸入任何內容,它仍會繼續。 –

+0

正如@Amit Agarwal試圖回答你的問題。我只是想讓你知道一個標準。 '元素不能共享一個ID。它會導致意想不到的行爲,特別是在使用javascript時。「您的單選按鈕具有相同的ID。請檢查;) –

+0

我很抱歉,但我很新,我不知道你的意思是什麼......哦,謝謝你艾米特它現在的作品! :) –

0

請嘗試更新的源。

<html> 
<head> 
    <title>Exam entry</title> 
    <script language="javascript" type="text/javascript"> 
    function validateForm(){ 
    var result = true; 
    var msg=""; 
    if (document.ExamEntry.name.value===''){ 
     msg+="You must enter your name \n"; 
     document.ExamEntry.name.focus(); 
     document.getElementById('name').style.color="red"; 
     result = false; 
    return false; 
    } else if (document.ExamEntry.subject.value==""){ 
     msg+="You must enter the subject \n"; 
     document.ExamEntry.subject.focus(); 
     document.getElementById('subject').style.color="red"; 
     result = false; 
    return false; 
    } else if (document.ExamEntry.number.value.length!="4") { 
     msg+="You must enter your exam number and it must be 4 digits long \n"; 
     document.ExamEntry.number.focus(); 
     document.getElementById('number').style.color="red"; 
     result = false; 
    return false; 
    } 
    var checked = null; 
    var inputs = document.getElementsByName('examtype'); 
    for (var i = 0; i < inputs.length; i++) { 
     if (inputs[i].checked) { 
     checked = inputs[i]; 
     break; 
     } 
    } 
    if(checked==null) 
    { 
    alert('Please choose an option'); 
    return false; 
    } 
    else{ 
    return confirm('You have chosen '+checked.value+' is this correct?'); 
    } 
    if(msg==""){ 
     return result; 
    } 
    else { 
     alert(msg); 
     return result; 
    } 
    } 
    </script> 
</head> 
<body> 
    <h1>Exam Entry Form</h1> 
    <form name="ExamEntry" method="post" action="success.html"> 
    <table width="50%" border="0"> 
    <tr> 
    <td id="name">Name</td> 
    <td><input type="text" name="name" /></td> 
    </tr> 
    <tr> 
    <td id="subject">Subject</td> 
    <td><input type="text" name="subject" /></td> 
    </tr> 
    <tr> 
    <td id="number">Exam Number</td> 
    <td><input type="text" name="number" /></td> 
    </tr> 
    <tr> 
    <td><input type="radio" id="examtype" name="examtype" value="GCSE">GCSE<br></td> 
    <td><input type="radio" id="examtype" name="examtype" value="AS">AS<br></td> 
    <td><input type="radio" id="examtype" name="examtype" value="A2">A2</td> 
    </tr> 
    <tr> 
    <td><input type="submit" name="Submit" value="Submit" 
     onclick="return validateForm();" /></td> 
    <td><input type="reset" name="Reset" value="Reset" /></td> 
    </tr> 
    </table> 
    </form> 
</body> 
</html>