0

这可能有点难以解释,我可能无法正确解释(我还没睡),如果是这样,我很抱歉,我会尽量让它更容易理解。我目前有一个禁用的复选框(选择)和一个​​文本框(参加)和一个未禁用的复选框(启用 2),我在表单 HTML 上方有一个功能,它适用于“启用 2”复选框,因此一旦它是检查它启用复选框“选择”。我想知道的是,一旦启用了“选择”复选框,然后您选中该框,它是否可以启用文本框“参加”并同时使该字段成为必填项?这是我当前的代码:

<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。

4

1 回答 1

0

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

var checkboxToMakeTextareamandatory = document.getElementByID(...);
var mandatoryTextarea= document.getElementByID(...);

if(checkboxToMakeTextareamandatory .checked && mandatoryTextarea.value == "") {
    alert(...);
}
于 2013-05-24T00:39:47.933 回答