2016-06-28 7 views
1

この調査フォームは完璧に機能しています。私はこの1つの質問を変更するだけでいいです。基本的にAnswersの「はい」と「いいえ」の2つのラジオボタンとその下にテキストエリアがあります。ユーザーが「はい」ラジオボタンを選択しない限り、テキスト領域に「はい」の理由を入力することができない限り、テキスト領域をロックします。ラジオボタンのテキストエリア

この機能を試してみましたが、動作していないようです。

<script> 
 
    function validate() { 
 
    var radioYes = document.getElementById('radioYes'); 
 
    var textarea = document.getElementById('question5comment'); 
 

 
    if (radioYes.checked && question5comment.value.length < 1) { 
 
     alert('Please enter your reason for Question 5.'); 
 
     question5comment.focus(); 
 
     return false; 
 
    } 
 
    } 
 

 
    function toggle(value) { 
 
    document.getElementById('question5comment').disabled = value; 
 
    } 
 
</script> 
 

 
5. Are you using other non-franchise service centres? 
 
<br> 
 
*if yes is there any other reason you would do so other than price 
 
<br> 
 
<input type="radio" name="question5" 
 
     value="Yes" required>Yes 
 
<br> 
 
<input type="radio" name="question5" 
 
     <value="No" required>No 
 
<br> 
 
<textarea name="question5comment" rows="5" cols="40" required></textarea>

答えて

0

あなたの構文が間違っている:value<不要

  1. いいえidあなたのコードを参照しています。
  2. 構文エラーです。

は、次を使用します。

function validate() { 
 
    var radioYes = document.getElementById('radioYes'); 
 
    var textarea = document.getElementById('question5comment'); 
 

 
    if (radioYes.checked && question5comment.value.length < 1) { 
 
    alert('Please enter your reason for Question 5.'); 
 
    question5comment.focus(); 
 
    document.getElementById('question5comment').disabled = true; 
 
    return false; 
 
    } else { 
 
    document.getElementById('question5comment').disabled = false; 
 
    } 
 
}
5. Are you using other non-franchise service centres? 
 
<br>*if yes is there any other reason you would do so other than price 
 
<br> 
 
<input type="radio" name="question5" value="Yes" required onclick="validate();" id="radioYes" />Yes 
 
<br> 
 
<input type="radio" name="question5" value="No" required onclick="validate();" id="radioNo" />No 
 
<br> 
 
<textarea name="question5comment" id="question5comment" rows="5" cols="40" required></textarea>

+0

本当に真偽を変更するだけでした。しかし、テキストエリアに何も入力せずに試して提出するだけで、警告がポップアップするようにしたいと思います。それは可能ですか? –

+0

はい、送信ハンドラで値の長さを確認し、それがyesである場合はチェックします。 –

+0

完璧な私はすべてのセットです。最後には、デフォルトでテキストエリアをロックすることができますか?フォームが開くと、テキスト領域がロックされます –

0

あなたは多くの行方不明のコードを持っています! idと同じように、関数が作成されますが、その関数は初期化されていません。

<script> 
function validate(t) { 
var question5comment = document.getElementById('question5comment');  
if (t.checked && question5comment.value.length < 1) { 
    alert('Please enter your reason for Question 5.'); 
    question5comment.focus(); 
    return false; 
} 
toggle(t.value); 
} 

function toggle(value) { 
    document.getElementById('question5comment').disabled = (value == "Yes") ? false : true; 
} 
</script> 


5. Are you using other non-franchise service centres? 
<br> 
*if yes is there any other reason you would do so other than price 
<br> 
<input type="radio" name="question5" 
value="Yes" onchange="validate(this)" required>Yes 
<br> 
<input type="radio" name="question5" onchange="validate(this)" value="No" required>No 
<br> 
<textarea name="question5comment" id="question5comment" rows="5" cols="40" required></textarea> 
関連する問題