Disabled form inputs do not appear in the request

后端 未结 11 1859
暗喜
暗喜 2020-11-22 04:37

I have some disabled inputs in a form and I want to send them to a server, but Chrome excludes them from the request.

Is there any workaround for this without addin

相关标签:
11条回答
  • 2020-11-22 05:08

    Elements with the disabled attribute are not submitted or you can say their values are not posted (see the second bullet point under Step 3 in the HTML 5 spec for building the form data set).

    I.e.,

    <input type="textbox" name="Percentage" value="100" disabled="disabled" /> 
    

    FYI, per 17.12.1 in the HTML 4 spec:

    1. Disabled controls do not receive focus.
    2. Disabled controls are skipped in tabbing navigation.
    3. Disabled controls cannot be successfully posted.

    You can use readonly attribute in your case, by doing this you will be able to post your field's data.

    I.e.,

    <input type="textbox" name="Percentage" value="100" readonly="readonly" />
    

    FYI, per 17.12.2 in the HTML 4 spec:

    1. Read-only elements receive focus but cannot be modified by the user.
    2. Read-only elements are included in tabbing navigation.
    3. Read-only elements are successfully posted.
    0 讨论(0)
  • 2020-11-22 05:09

    You can totally avoid disabling, it is painful since html form format won't send anything related to <p> or some other label.

    So you can instead put regular

    <input text tag just before you have `/>
    

    add this readonly="readonly"

    It wouldn't disable your text but wouldn't change by user so it work like <p> and will send value through form. Just remove border if you would like to make it more like <p> tag

    0 讨论(0)
  • 2020-11-22 05:10

    I find this works easier. readonly the input field, then style it so the end user knows it's read only. inputs placed here (from AJAX for example) can still submit, without extra code.

    <input readonly style="color: Grey; opacity: 1; ">
    
    0 讨论(0)
  • 2020-11-22 05:13

    Using Jquery and sending the data with ajax, you can solve your problem:

    <script>
    
    $('#form_id').submit(function() {
        $("#input_disabled_id").prop('disabled', false);
    
        //Rest of code
        })
    </script>
    
    0 讨论(0)
  • 2020-11-22 05:13

    In addition to Tom Blodget's response, you may simply add @HtmlBeginForm as the form action, like this:

     <form id="form" method="post" action="@Html.BeginForm("action", "controller", FormMethod.Post, new { onsubmit = "this.querySelectorAll('input').forEach(i => i.disabled = false)" })"
    
    0 讨论(0)
提交回复
热议问题