disable all the elements in html

后端 未结 8 1235
清歌不尽
清歌不尽 2020-12-14 09:13

How can we disable all the elements in html through javascript.The easiest way...

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 09:53

    All the form elements (inputs, selects, textareas) within a form, are accesible through the form.elements HTMLCollection, you can iterate the collection disabling each element:

    function disableForm(form) {
    var length = form.elements.length,
        i;
      for (i=0; i < length; i++) {
        form.elements[i].disabled = true;
      }
    }
    

    Usage examples:

    disableForm(document.forms[0]);
    disableForm(document.getElementById('formId'));
    

提交回复
热议问题