复选框使文本框成为必填项


Checkbox making textbox mandatory

这可能有点难以解释,我可能无法正确解释(我没有睡觉),如果是这样的话,我很抱歉,我会尽量让它尽可能容易理解。我目前有一个复选框(选择)和一个文本框(出席)被禁用和一个未禁用的复选框(enable2),我有一个功能在我的表单HTML上面,它在"enable2"复选框上工作,这样一旦它被选中,它就启用了复选框"选择"。我想知道如何做的是一旦"选择"复选框启用,然后选中该框,它可以启用文本框"出席",也使该字段是强制性的?下面是我当前的代码:

<script>
window.onload=function() {
document.getElementById('enable2').onchange=function(){
    var d = document.getElementById('box').childNodes;
    for( i=0, l=d.length; i<l; i++ )
    {
        d[i].disabled = !this.checked;
    }
};
}
</script>
<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return  ">
Enable Section D
<br>
<input type="checkbox" name="enable2" id="enable2">
<div id="box">
<b><p>Section D - Academically selective school</b></p>
<input type="checkbox" name="select" id="select" disabled />
<p>I have submitted an application for placement in an academically selective school. My child will be sitting for the Selective High Schools test. In case your child is not successful in gaining a place in this/these school(s), please also complete Section B,  Section C or Section E on this form.</p>
</div>
<div id="txts2">
<b><p>Section E - Placement not required</b></p>
<p>I will not be seeking to enrol my child in a NSW government school next year. My child will be attending the following school in 2015:*</p>
<input type="text" id="attend" name="attend" disabled />
<br>
<br>
Parent/Carer <input type="text" id="parentd" name="parentd" disabled />
<br>
Date <input type="text" id="datee" name="datee" disabled />
</div>
<input type="submit" name="submit" id="submit" value="submit" />
</form>
<script>
function validateCheckBoxes(theForm) {
if (!theForm.declare.checked) {
    alert ('You must tick the checkbox to confirm the declaration');
    return false;
} else {    
    return true;
}
}
var form = document.getElementById('eoi'),
validNumbers = [2474,
                2750,
                2753,
                2760,
                2777];
form.onsubmit = function() {
var userInput = document.getElementById("post"),
    numb = parseInt(userInput.value, 10);
if ( validNumbers.indexOf(numb) == -1 ) {
    alert("Please enter a correct postcode");
    return false;
}
return validateCheckBoxes(form);
}
</script>

如果那个解释不清楚,我很抱歉,只是问你是否需要任何澄清和任何帮助,这将是感激的,因为我只是学习javascript。

用这个来修改你的form.onsubmit = function() {,替换你的变量和id等等。

var checkboxToMakeTextareamandatory = document.getElementByID(...);
var mandatoryTextarea= document.getElementByID(...);
if(checkboxToMakeTextareamandatory .checked && mandatoryTextarea.value == "") {
    alert(...);
}