2013-09-25 29 views
0

如果單擊「是」按鈕,我正在嘗試執行的操作是重定向到新頁面。如果點擊「否」,我已經設置了提示。但是,有一個表單(姓名和電子郵件)需要填寫才能運行。如果他們沒有填寫這些詳細信息,我想提示一個提示,告訴用戶他們需要填寫它們才能繼續。Javascript:重定向到基於單選按鈕選項的新頁面

我相當新的JavaScript,所以任何提示或解釋將不勝感激。

下面是HTML代碼

<input type="radio" name="radio" id="yes"><strong>Yes, I agree.</strong> 
<br> 
<input type="radio" name="radio" id="no" checked="checked"><strong>No, I do not agree.   </strong><br> 
<br>  
If you agree, please enter your full name and email address in the spaces below and press submit. 
<br> 
<form> Full Name:<input type="text" name="fullname"></form> 
<br> 
<form> Email Address:<input type="text" name="email"></form> 
<br>  
<br> 
<form action="endPage.jsp" id="form"> 
<input type="submit" id="submit" value="Submit" name="submit" /> 
</form> 

和JavaScript代碼

var submitBtn = document.getElementById("submit"); 
var termsChk = document.getElementById("yes"); 
var formFrm = document.getElementById("emailField"); 
var formFrm = document.getElementById("nameField"); 

submitBtn.addEventListener("click", function() { 
if (termsChk.checked === true && formFrm) 

{ 
    alert("Checked!"); 
    formFrm.submit(); 
} else { 
    alert("Please Contact Spearhead Mining Corporation: [email protected] to discuss your options for project submittal!"); 
} 
return false; 
}); 
+2

顯示錶單的代碼。顯示「已設置」提示的代碼。 – acdcjunior

回答

0

對於更具體的答案,如果你發佈的是代碼並沒有任何按鍵可能有幫助,還有表單的HTML代碼。這就是說,你通常會處理這個問題的方式是在yes按鈕的代碼中運行你需要運行的任何客戶端驗證,在這種情況下檢查名稱和電子郵件字段是否爲空,然後顯示錯誤消息如果一切都無效,重定向。

比如在你的是處理

if(document.getElementById('emailField').value == null || document.getElementById('namefield') == null){ 
    /*error handling code goes here*/ 
    return 
} 
else{ 
    /*redirection code goes here*/ 
} 
+0

謝謝。我已經添加了迄今爲止所有的代碼。 – SkiGeo

相關問題