How can I make an entire HTML form “readonly”?

前端 未结 12 1403
无人及你
无人及你 2020-12-02 07:56

I have two pages with HTML forms. The first page has a submission form, and the second page has an acknowledgement form. The first form offers a choice of many controls, whi

相关标签:
12条回答
  • 2020-12-02 08:24

    You add html invisible layer over the form. For instance

    <div class="coverContainer">
    <form></form>
    </div>
    

    and style:

    .coverContainer{
        width: 100%;
        height: 100%;
        z-index: 100;
        background: rgba(0,0,0,0);
        position: absolute;
    }
    

    Ofcourse user can hide this layer in web browser.

    0 讨论(0)
  • 2020-12-02 08:25

    Not all form elements can be set to readonly, for example:

    • checkboxes
    • radio boxes
    • file upload
    • ...more..

    Then the reasonable solution would be to set all form elements' disabled attributes to true, since the OP did not state that the specific "locked" form should be sent to the server (which the disabled attribute does not allow).

    Another solution, which is presented in the demo below, is to place a layer on top of the form element which will prevent any interaction with all the elements inside the form element, since that layer is set with a greater z-index value:

    DEMO:

    var form = document.forms[0], // form element to be "readonly"
        btn1 = document.querySelectorAll('button')[0],
        btn2 = document.querySelectorAll('button')[1]
    
    btn1.addEventListener('click', lockForm)
    btn2.addEventListener('click', lockFormByCSS)
    
    function lockForm(){
      btn1.classList.toggle('on');
      [].slice.call( form.elements ).forEach(function(item){
          item.disabled = !item.disabled;
      });
    }
    
    function lockFormByCSS(){
      btn2.classList.toggle('on');
      form.classList.toggle('lock');
    }
    form{ position:relative; } 
    form.lock::before{
      content:'';
      position:absolute;
      z-index:999;
      top:0;
      right:0;
      bottom:0;
      left:0;
    }
    
    button.on{ color:red; }
    <button type='button'>Lock / Unlock Form</button>
    <button type='button'>Lock / Unlock Form (with CSS)</button>
    <br><br>
    <form>
      <fieldset>
        <legend>Some Form</legend>
        <input placeholder='text input'>
        <br><br>
        <input type='file'>
        <br><br>
        <textarea placeholder='textarea'></textarea>
        <br><br>
        <label><input type='checkbox'>Checkbox</label>
        <br><br>
        <label><input type='radio' name='r'>option 1</label>
        <label><input type='radio' name='r' checked>option 2</label>
        <label><input type='radio' name='r'>option 3</label>
        <br><br>
        <select>
          <option>options 1</option>
          <option>options 2</option>
          <option selected>options 3</option>
        </select>
      </fieldset>
    </form>

    0 讨论(0)
  • 2020-12-02 08:25

    Have all the form id's numbered and run a for loop in JS.

     for(id = 0; id<NUM_ELEMENTS; id++)
       document.getElementById(id).disabled = false; 
    
    0 讨论(0)
  • 2020-12-02 08:31

    On the confirmation page, don't put the content in editable controls, just write them to the page.

    0 讨论(0)
  • 2020-12-02 08:36

    You can use this function to disable the form:

    function disableForm(formID){
      $('#' + formID).children(':input').attr('disabled', 'disabled');
    }
    

    See the working demo here

    Note that it uses jQuery.

    0 讨论(0)
  • 2020-12-02 08:36

    There is no built-in way that I know of to do this so you will need to come up with a custom solution depending on how complicated your form is. You should read this post:

    Convert HTML forms to read-only (Update: broken post link, archived link)

    EDIT: Based on your update, why are you so worried about having it read-only? You can do it via client-side but if not you will have to add the required tag to each control or convert the data and display it as raw text with no controls. If you are trying to make it read-only so that the next post will be unmodified then you have a problem because anyone can mess with the post to produce whatever they want so when you do in fact finally receive the data you better be checking it again to make sure it is valid.

    0 讨论(0)
提交回复
热议问题