2016-05-26 12 views
0

私は顧客の調査に関する作業をしています。彼らが「はい」を選択した場合は、選択された方法の情報に&の連絡先の優先メソッドを要求する追加の質問が続きます。ラジオボタンを選択した場合はJavascriptを入力してください。追加入力が必要です

HTML

<b>May we contact you about your experience?</b> 
<br/> 
<input type="radio" name="Contact" id="ContactYes" value="Yes" required /> 
<label for="ContactYes">Yes</label> 
<input type="radio" name="Contact" id="ContactNo" value="No" /> 
<label for="ContactNo">No</label> 

<br /> 
<br /> 

<b>Select your preferred method of contact:</b> 
<br/> 
<input type="radio" name="FormOfContact" id="Phone" value="Phone" /> 
<label for="Phone">Phone</label> 
<input type="radio" name="FormOfContact" id="Email" value="Email" /> 
<label for="Email">Email</label> 

は基本的に、私が知りたいのですが、私が作ることができるか「FormOfContact」接触されると、ユーザが「はい」を選択する唯一の必須フィールドがありますか?

答えて

0

ラジオボタンのonchangeイベントを作成すると、「はい」または「いいえ」が選択されているかどうかを確認できます。 「はい」を選択した場合は、2番目のフィールドに属性「必須= true」を追加します。

ELEMENT.setAttribute("required",true) 
0

JavaScriptのビットが役立ちます。 ここにプランナーを見る:http://embed.plnkr.co/k3yIf0gh03JBACSeV6yA/

<!DOCTYPE html> 
<html> 

<script> 
function onChange(required) { 
    document.getElementById('Phone').required = required; 
    document.getElementById('div1').style.display = required ? 'block' : 'none'; 
} 
</script> 

<body> 
    <b>May we contact you about your experience?</b> 
    <br/> 
     <input type="radio" name="Contact" id="ContactYes" value="Yes" required onChange='onChange(true)'/><label for="ContactYes">Yes</label> 
     <input type="radio" name="Contact" id="ContactNo" value="No" onChange='onChange(false)' /><label for="ContactNo">No</label> 

    <br /> 
    <br /> 
    <div id='div1' style='display:none'> 
     <b>Select your preferred method of contact:</b><br/> 
     <input type="radio" name="FormOfContact" id="Phone" value="Phone" /><label for="Phone">Phone</label> 
     <input type="radio" name="FormOfContact" id="Email" value="Email" /><label for="Email">Email</label> 
    </div> 
</body> 
</html> 
関連する問題