2016-06-28 2 views
1

이 설문지는 완벽하게 정상적으로 작동합니다. 난이 질문 하나만 바꿔 줘야 해. 기본적으로 Answers "Yes"및 "No"에 대한 두 개의 라디오 버튼과 그 아래에 텍스트 영역이 있습니다. 이제 사용자가 "예"라디오 버튼을 선택하지 않으면 텍스트 영역을 잠그고 싶습니다. 텍스트 영역에 "예"라는 이유를 입력 할 수 있습니다.라디오 버튼 텍스트 영역

나는이 기능을 둘러 보았지만 시도했지만 작동하지 않는 것 같습니다.

<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. .
  2. 아니요 코드에서 참조하는 id입니다.
  3. 구문 오류입니다. 다음

사용 :

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

사실은 true와 false를 변경해야했습니다. 하지만 텍스트 영역에 아무 것도 입력하지 않고 시도하고 제출하는 경우에만 경고 메시지가 나타나기를 바랍니다. 가능한 선생님 이세요? –

+0

예, 제출 처리기에서 값의 길이를 확인한 다음 예인 경우를 확인하십시오. –

+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> 
관련 문제