0

私がやろうとしているのは、「はい」ボタンがクリックされた場合に新しいページにリダイレクトすることです。彼らが「いいえ」をクリックした場合、私はすでにプロンプ​​トを設定しています。ただし、これが機能するために記入する必要があるフォーム (名前と電子メール) があります。これらの詳細を入力しない場合は、先に進む前に入力する必要があることをユーザーに知らせるプロンプトが表示されるようにします。

私は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: projects@spearheadmining.com to discuss your options for project submittal!");
}
return false;
});
4

2 に答える 2

0

より具体的な回答については、はいボタンといいえボタンのコード、およびフォームの HTML コードを投稿すると役立つ場合があります。一般的にこれを処理する方法は、実行する必要があるクライアント側の検証を実行するはいボタンのコードにあります。この場合、名前と電子メールフィールドが空でないかどうかを確認し、代わりにエラーメッセージを表示しますすべてが有効でない場合のリダイレクト。

たとえば、あなたの yes ハンドラで

if(document.getElementById('emailField').value == null || document.getElementById('namefield') == null){
  /*error handling code goes here*/
  return
 }
else{
  /*redirection code goes here*/
}
于 2013-09-25T17:41:05.597 に答える
0

ユーザーへの特定のメッセージで window.confirm() を使用し、window.location()使用して新しい URL にリダイレクトします。

result = window.confirm("Message to user");
if(result) {
     window.location = "new url";
} else {
     //do the rest of logic
}
于 2013-09-25T17:41:53.410 に答える