禁用的表單輸入未出現在請求中

本文翻譯自:Disabled form inputs do not appear in the request

I have some disabled inputs in a form and I want to send them to a server, but Chrome excludes them from the request. 我在表單中有一些禁用的輸入,我想將它們發送到服務器,但是Chrome將其從請求中排除。

Is there any workaround for this without adding a hidden field? 是否有任何解決方法而不添加隱藏字段?

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />

    <!-- this does not appear in request -->
    <input type="textbox" name="Percentage" value="100" disabled="disabled" /> 

</form>

#1樓

參考:https://stackoom.com/question/UrxQ/禁用的表單輸入未出現在請求中


#2樓

If you absolutely have to have the field disabled and pass the data you could use a javascript to input the same data into a hidden field (or just set the hidden field too). 如果您絕對必須禁用該字段並傳遞數據,則可以使用javascript將相同的數據輸入到隱藏字段中(或者也可以設置隱藏字段)。 This would allow you to have it disabled but still post the data even though you'd be posting to another page. 這將允許您禁用它,但是即使您要發佈到另一個頁面,也仍然可以發佈數據。


#3樓

Using Jquery and sending the data with ajax, you can solve your problem: 使用Jquery並使用ajax發送數據,可以解決您的問題:

<script>

$('#form_id').submit(function() {
    $("#input_disabled_id").prop('disabled', false);

    //Rest of code
    })
</script>

#4樓

I'm updating this answer since is very useful. 我正在更新此答案,因爲它非常有用。 Just add readonly to the input. 只需將readonly添加到輸入中即可。

So the form will be: 因此,形式爲:

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />
    <input type="textbox" name="Percentage" value="100" readonly/>
</form>

#5樓

You can totally avoid disabling, it is painful since html form format won't send anything related to <p> or some other label. 您可以完全避免禁用它,這很痛苦,因爲html表單格式不會發送與<p>或其他標籤有關的任何內容。

So you can instead put regular 所以你可以放常規

<input text tag just before you have `/>

add this readonly="readonly" 添加此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. 它不會禁用您的文本,但不會被用戶更改,因此它像<p>一樣工作,並將通過表單發送值。 Just remove border if you would like to make it more like <p> tag 如果您想使邊框更像<p>標記,請刪除邊框


#6樓

To post values from disabled inputs in addition to enabled inputs, you can simply re-enable all of the form's inputs as it is being submitted. 要發佈來自啓用的輸入之外的來自禁用輸入的值,您可以在提交表單時簡單地重新啓用所有表單輸入。

<form onsubmit="this.querySelectorAll('input').forEach(i => i.disabled = false)">
    <!-- Re-enable all input elements on submit so they are all posted, 
         even if currently disabled. -->

    <!-- form content with input elements -->
</form>

If you prefer jQuery: 如果您更喜歡jQuery:

<form onsubmit="$(this).find('input').prop('disabled', false)">
    <!-- Re-enable all input elements on submit so they are all posted, 
         even if currently disabled. -->

    <!-- form content with input elements -->
</form>

For ASP.NET MVC C# Razor, you add the submit handler like this: 對於ASP.NET MVC C#Razor,您可以如下添加提交處理程序:

using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post,
    // Re-enable all input elements on submit so they are all posted, even if currently disabled.
    new { onsubmit = "this.querySelectorAll('input').forEach(i => i.disabled = false)" } ))
{
    <!-- form content with input elements -->
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章