How to submit the value of a disabled form field in a form Example code

How to submit the value of a disabled form field in a form Example code
If a form field in a form is set to disabled, the value of the form field will not be submitted. But sometimes you do need to submit this value.
There are many solutions, such as using readonly instead of disabled, but I think none of them are very good (the specific reasons are omitted).

I thought of a solution, which is to copy the form to be submitted before submitting it, then set the disabled attribute of all form fields in the copied form to false, and then submit the copied form. The following is the demonstration code (save the code to a file named submit.html to observe the demonstration effect).

Copy code
The code is as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift-jis">
<title>
Submit form
</title>
<script>
function doSubmit(form) {
var newForm = form.cloneNode(true);
enableFields(newForm.getElementsByTagName("input"));
enableFields(newForm.getElementsByTagName("textarea"));
enableFields(newForm.getElementsByTagName("select"));
newForm.style.display = "none";
document.body.appendChild(newForm);
newForm.submit();
}
function enableFields(fields) {
for(var i=0;i<fields.length;i++) {
var field = fields[i];
if (field instanceof(Array)) {
for (var j=0;j<field.length;j++) {
field[j].disabled = false;
}
} else {
field.disabled = false;
}
}
}
</script>
</head>
<body>
<form action="submit.html">
<input type="text" name="text" value="tt" disabled/>

<input type="radio" name="radio" value="r1" checked disabled>r1
<input type="radio" name="radio" value="r2">r2

<input type="checkbox" name="checkbox" value="c1">c1
<input type="checkbox" name="checkbox" value="c2" checked disabled>c2

<select name="select" disabled>
<option value="1">1</option>
<option value="2">2</option>
</select>

<textarea name="textarea" disabled>123</textarea>

<input type="button" value="submit" onclick="doSubmit(this.form)">
</form>
</body>

<<:  CSS3 realizes the graphic falling animation effect

>>:  Detailed explanation of ES6 Promise usage

Recommend

W3C Tutorial (13): W3C WSDL Activities

Web Services are concerned with application-to-ap...

Three ways to configure Nginx virtual hosts (based on domain names)

Nginx supports three ways to configure virtual ho...

How to implement Linux deepin to delete redundant kernels

The previous article wrote about how to manually ...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

Example of implementing TikTok text shaking effect with CSS

In daily development, front-end students often ar...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

MySQL 5.7.18 installation tutorial under Windows

This article explains how to install MySQL from a...

js to call the network camera and handle common errors

Recently, due to business reasons, I need to acce...

MySQL restores data through binlog

Table of contents mysql log files binlog Binlog l...

How to install docker using YUM

As shown in the following figure: If the version ...

Specific use of CSS content attribute

The content attribute is generally used in the ::...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

WeChat Mini Program QR Code Generation Tool weapp-qrcode Detailed Explanation

WeChat Mini Program - QR Code Generator Download:...

Detailed explanation of meta tags (the role of meta tags)

No matter how wonderful your personal website is,...