0
<tr>
<td><b>If the registration is for either a Consultant or End User Business</b></td>
    <td>&nbsp;</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>

如果提供答案,则勾选复选框

更新:这是文本字段的代码

    <tr> 
      <td align="right"><strong>Consulting Company Name: </strong><font color="red">*</font></td> 
      <td><input size="30" name="ConCompany" class="required"/></td> 
   </tr>
4

2 回答 2

0

在第一步中,我会在您的文本字段中添加一个 id,您可以通过 JavaScript 添加一个事件处理程序。

  <td><input id="company" size="30" name="ConCompany" class="required"/></td> 

添加事件处理程序:

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必需属性而不是您的“必需”类,请将最后一个 for 循环更改为以下内容:

  for (var j = 0; j < allCheckboxes.length; j++) {
    tmpCheckbox = allCheckboxes[j];
    tmpCheckbox.required = bSetRequired;
  }
于 2013-11-12T10:54:42.420 回答
0

使用button带有id类似submitbtn按钮的 a

<input type="button" id="submitbtn" value="Submit" />

添加id到你的textbox喜欢id=ConCompany

dd ids 到您的复选框中,如下所示:

<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();
 }
于 2013-11-12T09:58:39.193 回答