This article describes the js solution to the disabled problem of web form submission. Share it for your reference. The specific analysis is as follows:
For example, there is a form as follows
Copy the code code as follows:<form id="inputForm" action="shorttermrental.action" method="post">
<input name="pname" type="text" id="pname" value="xxx" size="20" disabled="disabled"/>
<input name="but" type="submit" id="but" value="xxx" size="20"/>
</form>
When we submit the form, we cannot get the pname data in the background because the property of the input box is disabled. There are the following solutions for this function:
1. Change the disabled="disabled" attribute to readonly="readonly" instead. Its function is basically the same, readonly can also support onfocus events. (Recommended use)
Copy the code code as follows:<input name="xxx" id="xxx" value="xxx" size="20" readonly="readonly"/>
2. Use js to modify the disabled attribute when submitting form
Copy the code code as follows:<input name="but" type="button" id="but" value="xxx" size="20"/>
<script type="text/javascript">
function submit_form(){
//Javascript writing method
document.getElementById("pname").disabled="";
document.getElementById("inputForm").submit();
// jQuery writing method
$("#pname").attr("disabled",false);
$("#inputForm").submit();
}
</script>
3. Get the value of pname in js, pass it in parameter form, and modify the action value of form. Of course, in this way, you need to modify the background code to get the parameter (getParameter);
Copy the code as follows:<script type="text/javascript">
function submit_form(){
// jQuery writing method
var pname = $("#pname").val();
$("#inputForm").attr("action","shorttermrental.action?panme="+pname+"&p="+new Date());
$("#inputForm").submit();
//Javascript writing method
var pname = document.getElementById("pname").value;
document.getElementById("pname").action = "shorttermrental.action?panme="+pname+"&p="+new Date();
document.getElementById("pname").submit();
}
</script>
A little tip: When we write scripting languages, we are generally keen on jQuery writing, because it is very concise. jQuery encapsulates javascript and uses regular expression matching to obtain when taking HTML equivalent values.
Therefore, it will inevitably affect efficiency, so it is recommended that you generally use javascript when there are a large number of scripts.
I hope this article will be helpful to everyone's JavaScript programming.