这可能有点难以解释,我可能无法正确解释(我没有睡觉)如果是这样我很抱歉我会尽量让它尽可能容易理解。我目前有一个复选框(选择)和一个禁用的文本框(参加)和一个未禁用的复选框(启用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。
答案 0 :(得分:0)
只需使用此修改您的form.onsubmit = function() {
,替换您的vars和ID等。
var checkboxToMakeTextareamandatory = document.getElementByID(...);
var mandatoryTextarea= document.getElementByID(...);
if(checkboxToMakeTextareamandatory .checked && mandatoryTextarea.value == "") {
alert(...);
}