This article describes the disabled problem of Web Form submission JS solution. Share to everyone for your reference. The specific analysis is as follows:
For example, like the following form
Copy Code code as follows:
<form id= "Inputform" action= "shorttermrental.action" method= "POST" >
<input name= "PName" type= "text" id= "pname" value= "xxx" "size=" disabled= "disabled"/>
<input name= "But" type= "submit" id= "but" value= "xxx" size= "/>"
</form>
When we submit the form, we cannot get pname data in the background because the property of the input box disabled. That is, can save the value can also keep the user can not enter this feature has the following solutions:
1. Replace the disabled= "disabled" Property with Readonly= "ReadOnly", which is essentially the same, and ReadOnly can support onfocus events. (Recommended use)
Copy Code code as follows:
<input name= "xxx" id= "xxx" value= "xxx" "size=" readonly= "readonly"/>
2. Use JS to modify the Disabled property when form is submitted
Copy Code code as follows:
<input name= "But" type= "button" id= "but" value= "xxx" size= "/>"
<script type= "Text/javascript" >
function Submit_form () {
JavaScript notation
document.getElementById ("PName"). disabled= "";
document.getElementById ("Inputform"). Submit ();
jquery notation
$ ("#pname"). attr ("disabled", false);
$ ("#inputForm"). Submit ();
}
</script>
3. The value of the pname will be obtained using JS, pass the Parameter form, modify the action value of the form, of course, it needs to modify the background code, get the parameter (getparameter);
Copy Code code as follows:
<script type= "Text/javascript" >
function Submit_form () {
jquery notation
var pname = $ ("#pname"). Val ();
$ ("#inputForm"). attr ("Action", "Shorttermrental.action?panme=" +pname+ "&p=" +new Date ());
$ ("#inputForm"). Submit ();
JavaScript notation
var pname = document.getElementById ("PName"). Value;
document.getElementById ("PName"). Action = "shorttermrental.action?panme=" +pname+ "&p=" +new Date ();
document.getElementById ("PName"). Submit ();
}
</script>
A little piece of advice: when we write scripting languages, we tend to be passionate about jquery writing because it's conciseness, jquery encapsulates JavaScript, takes HTML equivalents, uses regular expression matching to get,
Therefore, it is necessary to influence efficiency, so we recommend that when there is a large number of scripts, generally use JavaScript.
I hope this article will help you with your JavaScript programming.