<tr>
<td><b>If the registration is for either a Consultant or End User Business</b></td>
<td> </td> <td>Please tick box (multiple choice) their area of business</td></tr Question being asked
<td align="right"><b>Consultant or End User: </b>
<td><font color="red">*</font></td>
<td><input type="checkbox" name="Data" value="Yes">Data Centres
<br />
<input type="checkbox" name="Power" value="Yes">Power Plants
<br />
<input type="checkbox" name="Mining" value="Yes">Mining
<br />
<input type="checkbox" name="Telecom" value="Yes">Telecom
<br />
<input type="checkbox" name="Governmental" value="Yes">Governmental
<br />
<input type="checkbox" name="Airports" value="Yes">Airports
<br />
<input type="checkbox" name="Hotel" value="Yes">Hotel/Residential
<br />
<input type="checkbox" name="Healthcare" value="Yes">Healthcare
<br />
<input type="checkbox" name="Shopping" value="Yes">Shopping Complex
<br />
<input type="checkbox" name="Industries" value="Yes">Industries / Manufacturing
<br />
<input type="checkbox" name="Transport" value="Yes">Transport
<br />
<input type="checkbox" name="Utilities" value="Yes">Utilities
<br />
<input type="checkbox" name="Water" value="Yes">Water Treatment
<br />
<input type="checkbox" name="Construction" value="Yes">Construction
<br />
<input type="checkbox" name="Other" value="Yes">Other
<br />
</td> </tr> <tr> <td colspan="3" align="center" bgcolor="#dadada"> </tr>
<tr>
如果提供答案,则勾选复选框
更新:这是textfield的代码
<tr>
<td align="right"><strong>Consulting Company Name: </strong><font color="red">*</font></td>
<td><input size="30" name="ConCompany" class="required"/></td>
</tr>
答案 0 :(得分:0)
使用button
和id
之类的submitbtn
按钮
<input type="button" id="submitbtn" value="Submit" />
将id
添加到您的textbox
,例如id=ConCompany
dd id
到您的复选框,如下所示:
<input type="checkbox" name="Data" id="10" value="Yes">Data Centres
<br />
<input type="checkbox" name="Power" id="11" value="Yes">Power Plants
<br />
<input type="checkbox" name="Mining" id="12" value="Yes">Mining
<br />
<input type="checkbox" name="Telecom" id="13" value="Yes">Telecom
<br />
<input type="checkbox" name="Governmental" id="14" value="Yes">Governmental
<br />
<input type="checkbox" name="Airports" id="15" value="Yes">Airports
<br />
<input type="checkbox" name="Hotel" id="16" value="Yes">Hotel/Residential
<br />
<input type="checkbox" name="Healthcare" id="17" value="Yes">Healthcare
<br />
<input type="checkbox" name="Shopping" id="18" value="Yes">Shopping Complex
<br />
<input type="checkbox" name="Industries" id="19" value="Yes">Industries / Manufacturing
<br />
<input type="checkbox" name="Transport" id="20" value="Yes">Transport
<br />
<input type="checkbox" name="Utilities" id="21" value="Yes">Utilities
<br />
<input type="checkbox" name="Water" id="22" value="Yes">Water Treatment
<br />
<input type="checkbox" name="Construction" id="23" value="Yes">Construction
现在使用以下javascript:
document.getElementById('submitbtn').onclick = function () {
if(document.getElementById('ConCompany').value != "")
{
var count = 0;
for ( var i = 10 ; i <= 23 ; i++)
{
if(document.getElementById(i).value == "Yes")
count++;
}
if(count == 0)
{
alert("Please select the area of business ");
return;
}
}
document.FormName.submit();
}
答案 1 :(得分:0)
在第一步中我会在你的文本字段中添加一个id,你可以通过JavaScript添加一个事件处理程序。
<td><input id="company" size="30" name="ConCompany" class="required"/></td>
添加Eventhandler:
document.getElementById('company').addEventListener('change', setCheckboxesRequired);
创建功能以设置所需的所有复选框:
function setCheckboxesRequired() {
var allInputs, allCheckboxes, textValue, bSetRequired, tmpInput, tmpCheckbox;
// Retrieve all input elements
allInputs = document.getElementsByTagName('input');
allCheckboxes = [];
// put all inputs with type = checkbox in allCheckboxes variable
for (var i = 0; i < allInputs.length; i++) {
tmpInput = allInputs[i];
if (tmpInput.type === 'checkbox') {
allCheckboxes.push(tmpInput);
}
}
// determine if class should be set
textValue = document.getElementById('company').value;
if (textValue === null || textValue === "") {
bSetRequired = false;
} else {
bSetRequired = true;
}
// iteration through all checkboxes
for (var j = 0; j < allCheckboxes.length; j++) {
tmpCheckbox = allCheckboxes[j];
// set or delete class
if (bSetRequired) {
tmpCheckbox.className = tmpCheckbox.className + ' required';
} else {
tmpCheckbox.className = tmpCheckbox.className.replace('required', '');
}
}
}
或者,如果您要设置HTML5 required-attribute而不是您的&#39; -class,请将最后一个for循环更改为以下内容:
for (var j = 0; j < allCheckboxes.length; j++) {
tmpCheckbox = allCheckboxes[j];
tmpCheckbox.required = bSetRequired;
}