纯JavaScript来禁用div中的所有表单元素

时间:2017-05-05 13:45:51

标签: javascript

有没有办法通过使用纯javascript而不是jquery或angular来告诉父div名称来禁用表单中的所有字段(textarea / textfield / option / input / checkbox / submit等)?

我想要纯粹的javascript,因为我刚刚开始使用JS,我发现所有不同的排列都很混乱。

我目前正在做类似的事情,但希望简化这一点:

  <script type="text/javascript" charset="utf-8">
    ( document.onclick = function() {
      var clickNoElements = [ 'juniperPresentNo', 'customerJuniperPresentNo', 'mpr95001PresentNo', 'mpr95001PresentNo', 'noranTelPresentNo', 'powerPlantPresentNo', 'batteryRackCabinetPresentNo', 'bbu1PresentNo', 'siteAlarmMdfPresentNo', 'towerLightsPresentNo', 'generatorPresentNo', 'tlanPresentNo' ];

      var clickYesElements = [ 'juniperPresentYes', 'customerJuniperPresentYes', 'mpr95001PresentYes', 'mpr95001PresentYes', 'noranTelPresentYes', 'powerPlantPresentYes', 'batteryRackCabinetPresentYes', 'bbu1PresentYes', 'siteAlarmMdfPresentYes', 'towerLightsPresentYes', 'generatorPresentYes', 'tlanPresentYes' ];

      var disableEnableElements = [ 'buttonJuniperChecks', 'juniperChecksOk', 'juniperChecksNok', 'buttonBellCeJuniperChecks', 'customerJuniperChecksOk', 'bellCeJuniperChecksNok', 'buttonMpr95001Checks', 'mpr95001ChecksOk', 'mpr95001ChecksNok', 'buttonMpr95001Checks', 'mpr95001ChecksOk', 'mpr95001ChecksNok', 'buttonNoranTelChecks', 'noranTelChecksOk', 'noranTelChecksNok', 'buttonPowerPlantChecks', 'powerPlantChecksOk', 'powerPlantChecksNok', 'buttonBatteryRackCabinetChecks', 'batteryRackCabinetChecksOk', 'batteryRackCabinetChecksNok', 'buttonBbu1Checks', 'bbu1ChecksOk', 'bbu1ChecksNok', 'buttonSiteAlarmMdfChecks', 'siteAlarmMdfChecksOk', 'siteAlarmMdfChecksNok', 'buttonTowerLightsChecks', 'towerLightsChecksOk', 'towerLightsChecksNok', 'buttonGeneratorChecks', 'generatorChecksOk', 'generatorChecksNok', 'buttonTlanChecks', 'tlanPresentChecksOk', 'tlanPresentChecksNok' ];
    })();

    function mobilityJuniperPresent() {
      if ( document.getElementById( 'mobilityJuniperPresentNo' ).checked ) {
        document.getElementById( 'buttonMobilityJuniperChecks' ).disabled = true;
        document.getElementById( 'mobilityJuniperChecksOk' ).disabled = true;
        document.getElementById( 'mobilityJuniperChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonMobilityJuniperChecks' ).disabled = false;
        document.getElementById( 'mobilityJuniperChecksOk' ).disabled = false;
        document.getElementById( 'mobilityJuniperChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      mobilityJuniperPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "mobilityJuniperPresent" ]' ).on( "click", mobilityJuniperPresent );
    }); // close docReady FUNC

    function customerJuniperPresent() {
      if ( document.getElementById( 'customerJuniperPresentNo' ).checked ) {
        document.getElementById( 'buttonCustomerJuniperChecks' ).disabled = true;
        document.getElementById( 'customerJuniperChecksOk' ).disabled = true;
        document.getElementById( 'customerJuniperChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonCustomerJuniperChecks' ).disabled = false;
        document.getElementById( 'customerbellceJuniperChecksOk' ).disabled = false;
        document.getElementById( 'customerJuniperChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      customerJuniperPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "customerJuniperPresent" ]' ).on( "click", customerJuniperPresent );
    }); // close docReady FUNC

    function mpr95001Present() {
      if ( document.getElementById( 'mpr95001PresentNo' ).checked ) {
        document.getElementById( 'buttonMpr95001Checks' ).disabled = true;
        document.getElementById( 'mpr95001ChecksOk' ).disabled = true;
        document.getElementById( 'mpr95001ChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonMpr95001Checks' ).disabled = false;
        document.getElementById( 'mpr95001ChecksOk' ).disabled = false;
        document.getElementById( 'mpr95001ChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      mpr95001Present(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "mpr95001Present" ]' ).on( "click", mpr95001Present );
    }); // close docReady FUNC

    function mpr95002Present() {
      if ( document.getElementById( 'mpr95002PresentNo' ).checked ) {
        document.getElementById( 'buttonMpr95002Checks' ).disabled = true;
        document.getElementById( 'mpr95002ChecksOk' ).disabled = true;
        document.getElementById( 'mpr95002ChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonMpr95002Checks' ).disabled = false;
        document.getElementById( 'mpr95002ChecksOk' ).disabled = false;
        document.getElementById( 'mpr95002ChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      mpr95002Present(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "mpr95002Present" ]' ).on( "click", mpr95002Present );
    }); // close docReady FUNC

    function noranTelPresent() {
      if ( document.getElementById( 'noranTelPresentNo' ).checked ) {
        document.getElementById( 'buttonNoranTelChecks' ).disabled = true;
        document.getElementById( 'noranTelChecksOk' ).disabled = true;
        document.getElementById( 'noranTelChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonNoranTelChecks' ).disabled = false;
        document.getElementById( 'noranTelChecksOk' ).disabled = false;
        document.getElementById( 'noranTelChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      noranTelPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "noranTelPresent" ]' ).on( "click", noranTelPresent );
    }); // close docReady FUNC

    function powerPlantPresent() {
      if ( document.getElementById( 'powerPlantPresentNo' ).checked ) {
        document.getElementById( 'buttonPowerPlantChecks' ).disabled = true;
        document.getElementById( 'powerPlantChecksOk' ).disabled = true;
        document.getElementById( 'powerPlantChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonPowerPlantChecks' ).disabled = false;
        document.getElementById( 'powerPlantChecksOk' ).disabled = false;
        document.getElementById( 'powerPlantChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      powerPlantPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "powerPlantPresent" ]' ).on( "click", powerPlantPresent );
    }); // close docReady FUNC

    function batteryRackCabinetPresent() {
      if ( document.getElementById( 'batteryRackCabinetPresentNo' ).checked ) {
        document.getElementById( 'buttonBatteryRackCabinetChecks' ).disabled = true;
        document.getElementById( 'batteryRackCabinetChecksOk' ).disabled = true;
        document.getElementById( 'batteryRackCabinetChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonBatteryRackCabinetChecks' ).disabled = false;
        document.getElementById( 'batteryRackCabinetChecksOk' ).disabled = false;
        document.getElementById( 'batteryRackCabinetChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      batteryRackCabinetPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "batteryRackCabinetPresent" ]' ).on( "click", batteryRackCabinetPresent );
    }); // close docReady FUNC

    function bbu1Present() {
      if ( document.getElementById( 'bbu1PresentNo' ).checked ) {
        document.getElementById( 'buttonBbu1Checks' ).disabled = true;
        document.getElementById( 'bbu1ChecksOk' ).disabled = true;
        document.getElementById( 'bbu1ChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonBbu1Checks' ).disabled = false;
        document.getElementById( 'bbu1ChecksOk' ).disabled = false;
        document.getElementById( 'bbu1ChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      bbu1Present(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "bbu1Present" ]' ).on( "click", bbu1Present );
    }); // close docReady FUNC

    function bbu2Present() {
      if ( document.getElementById( 'bbu2PresentNo' ).checked ) {
        document.getElementById( 'buttonBbu2Checks' ).disabled = true;
        document.getElementById( 'bbu2ChecksOk' ).disabled = true;
        document.getElementById( 'bbu2ChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonBbu2Checks' ).disabled = false;
        document.getElementById( 'bbu2ChecksOk' ).disabled = false;
        document.getElementById( 'bbu2ChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      bbu2Present(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "bbu2Present" ]' ).on( "click", bbu2Present );
    }); // close docReady FUNC

    function bbu3Present() {
      if ( document.getElementById( 'bbu3PresentNo' ).checked ) {
        document.getElementById( 'buttonBbu3Checks' ).disabled = true;
        document.getElementById( 'bbu3ChecksOk' ).disabled = true;
        document.getElementById( 'bbu3ChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonBbu3Checks' ).disabled = false;
        document.getElementById( 'bbu3ChecksOk' ).disabled = false;
        document.getElementById( 'bbu3ChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      bbu3Present(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "bbu3Present" ]' ).on( "click", bbu3Present );
    }); // close docReady FUNC

    function mdfPresent() {
      if ( document.getElementById( 'mdfPresentNo' ).checked ) {
        document.getElementById( 'buttonSiteAlarmMdfChecks' ).disabled = true;
        document.getElementById( 'siteAlarmMdfChecksOk' ).disabled = true;
        document.getElementById( 'siteAlarmMdfChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonSiteAlarmMdfChecks' ).disabled = false;
        document.getElementById( 'siteAlarmMdfChecksOk' ).disabled = false;
        document.getElementById( 'siteAlarmMdfChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      mdfPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "mdfPresent" ]' ).on( "click", mdfPresent );
    }); // close docReady FUNC

    function towerLightsPresent() {
      if ( document.getElementById( 'towerLightsPresentNo' ).checked ) {
        document.getElementById( 'buttonTowerLightsChecks' ).disabled = true;
        document.getElementById( 'towerLightsChecksOk' ).disabled = true;
        document.getElementById( 'towerLightsChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonTowerLightsChecks' ).disabled = false;
        document.getElementById( 'towerLightsChecksOk' ).disabled = false;
        document.getElementById( 'towerLightsChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      towerLightsPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "towerLightsPresent" ]' ).on( "click", towerLightsPresent );
    }); // close docReady FUNC

    function generatorPresent() {
      if ( document.getElementById( 'generatorPresentNo' ).checked ) {
        document.getElementById( 'buttonGeneratorChecks' ).disabled = true;
        document.getElementById( 'generatorChecksOk' ).disabled = true;
        document.getElementById( 'generatorChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonGeneratorChecks' ).disabled = false;
        document.getElementById( 'generatorChecksOk' ).disabled = false;
        document.getElementById( 'generatorChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      generatorPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "generatorPresent" ]' ).on( "click", generatorPresent );
    }); // close docReady FUNC

    function tlanPresent() {
      if ( document.getElementById( 'tlanPresentNo' ).checked ) {
        document.getElementById( 'buttonTlanChecks' ).disabled = true;
        document.getElementById( 'tlanChecksOk' ).disabled = true;
        document.getElementById( 'tlanChecksNok' ).disabled = true;
      } else {
        document.getElementById( 'buttonTlanChecks' ).disabled = false;
        document.getElementById( 'tlanChecksOk' ).disabled = false;
        document.getElementById( 'tlanChecksNok' ).disabled = false;
      } // close IF
    } // close FUNC
    $( function() { // when page loads
      tlanPresent(); // run FUNC
      // when either radio is clicked, run the function
      $( 'input[ name = "tlanPresent" ]' ).on( "click", tlanPresent );
    }); // close docReady FUNC

  </script>

我知道它非常臃肿而且效率不高,但就像我说我刚开始使用JS一样,所以我有很多东西要学习改进。

4 个答案:

答案 0 :(得分:3)

这是使用Vanilla (纯) JS的解决方案:

var form  = document.getElementById("formId");
var allElements = form.elements;
for (var i = 0, l = allElements.length; i < l; ++i) {
    // allElements[i].readOnly = true;
       allElements[i].disabled=true;
}

for循环解析表单中的所有元素,并将true设置为disabled(或readOnly)属性。这使得表单中的所有内容禁用

这是一个小提琴:

&#13;
&#13;
var form  = document.getElementById("formId");
var allElements = form.elements;
for (var i = 0, l = allElements.length; i < l; ++i) {
    // allElements[i].readOnly = true; 
       allElements[i].disabled=true;
}
&#13;
<form id="formId">
  <textarea>write sth</textarea><br>
  <input type="text" /><br>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

使用此document.querySelectorAll('form > *')。将选择表单的所有innerElement

&#13;
&#13;
document.querySelectorAll('form > *').forEach(function(a){
a.disabled=true;
})
&#13;
<form>
<input>
<input>
<input>
<select></select>
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用document.getElementsByTagName("form").disabled = true;

答案 3 :(得分:0)

你可以创建一个循环来获取表单中的所有元素,然后在下面添加代码:

int lastPos;
if(linkHref.indexOf("?") > 0) {
   lastPos = linkHref.indexOf("?");
} else if (linkHref.indexOf("&") > 0){
   lastPos = linkHref.indexOf("&");
}
else lastPos = -1;

if(lastPos != -1)
linkHref = linkHref.subsring(0, lastPos);