2016-06-15 4 views
0

나는 간단한 설문지를 디자인했지만 어떤 이유로 인해 한 줄의 코드가 작동하지 않습니다.Textarea locking

5. 사용자가 라디오 버튼을 예로 선택한 경우 텍스트 영역에 무언가를 입력해야합니다. 그렇지 않으면 양식을 보내서는 안됩니다.

그렇다면 텍스트 영역이 잠겨있어서 아무것도 입력 할 수 없습니다. 여기

코드입니다 : 도움말 숙녀를위한 감사합니다 신사

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" 
<?php if (isset($question5) && $question5=="Yes") echo "checked";?> 
value="Yes">Yes 
<br> 
<input type="radio" name="question5" 
<?php if (isset($question5) && $question5=="No") echo "checked";?> 
value="No">No 
<br> 
<textarea name="question5" rows="5" cols="40"><?php echo $question5 </textarea> 
+2

PHP가 서버에서 실행됩니다. 그것은 클라이언트로 보내진 후 폼으로 아무 것도 할 수 없습니다. 자바 스크립트가 필요합니다. –

+0

@MarcB 선생님에게 보내기 전에 말입니다. 양식은 질문 5에 예가있는 경우 텍스트 영역에 무엇인가를 입력해야한다는 것을 알아야합니다. 아니면 텍스트 영역이 잠 깁니다. –

+0

JavaScript로 시도하십시오 –

답변

1

옵션 1 : 텍스트 영역을 숨길

function check5(selectedType){ 
 
    if (selectedType == 'Yes'){ 
 
    document.getElementById('5textarea').style.display = 'block'; 
 
\t 
 
    } else{ 
 
    
 
    document.getElementById('5textarea').style.display = 'none'; 
 
    
 
    } 
 
}
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" onclick="check5(this.value)"> Yes 
 
<br> 
 
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No 
 
<br> 
 

 
<textarea id="5textarea" name="question5" rows="5" cols="40" style="display:none"></textarea>

옵션 2 : 텍스트 영역을 활성화/비활성화 (테스트하지 않음) :

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" onclick="check5(this.value)"> Yes 
<br> 
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No 
<br> 
<textarea id="5textarea" name="question5" rows="5" cols="40" disabled="true"></textarea> 

과에 자바 스크립트를 변경 :

function check5(selectedType){ 
if (selectedType == 'Yes'){ 
    document.getElementById("5textarea").disabled = false; 

} else{ 

    document.getElementById("5textarea").disabled = true; 
} 
} 
0

당신은 PHP는 서버에서 실행하기 때문에 자바 스크립트와 함께 할 필요가, 하지 브라우저에서!

이 코드에서

필요하지 않은 경우, 텍스트 영역은 비활성화되고 "예"를 선택하고 텍스트 영역이 충분한 텍스트를 포함하지 않는 경우 형태로 제출하면 오류가 나타납니다

// Javascript: 
 
function validate() { 
 
    var radioYes = document.getElementById('radioYes'); 
 
    var textarea = document.getElementById('textarea'); 
 
    
 
    if (radioYes.checked && textarea.value.length < 10) { 
 
     alert('Enter at least 10 characters!'); 
 
     textarea.focus(); 
 
     return false; 
 
    } 
 
} 
 

 
function toggle(value) { 
 
\t document.getElementById('textarea').disabled = value; 
 
}
/* CSS: */ 
 
* { 
 
    font-family: sans-serif; 
 
    margin: 4px; 
 
}
<!-- HTML/PHP: --> 
 
<form action="#"> 
 
    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="toggle(this.selected)" id="radioYes">Yes<br> 
 
    <input type="radio" name="question5" value="No" onchange="toggle(!this.selected)">No<br> 
 
    
 
    <textarea id="textarea" name="question5" rows="5" cols="40" disabled></textarea><br> 
 

 
    <input type="submit" value="Send" onclick="return validate()" /> 
 
</form>

JSFiddle