How to solve the disabled problem of Web form submission in js, and how to solve the disabledjs problem in web form

Source: Internet
Author: User

How to solve the disabled problem of Web form submission in js, and how to solve the disabledjs problem in web form

This article describes how to solve the disabled problem js in Web form submission. Share it with you for your reference. The specific analysis is as follows:

For example, there are the following forms:
Copy codeThe Code is as follows: <form id = "inputForm" action = "tertermrental. 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 a form, pname data cannot be obtained in the background, because the attribute of this input box is disabled. You can save the value and cannot enter this function in the following ways:

1. Replace the disabled = "disabled" attribute with readonly = "readonly". The feature is basically the same, and readonly supports onfocus events. (Recommended)
Copy codeThe Code is as follows: <input name = "xxx" id = "xxx" value = "xxx" size = "20" readonly = "readonly"/>
2. When submitting a form, use js to modify the disabled attribute.
Copy codeThe Code is as follows: <input name = "but" type = "button" id = "but" value = "xxx" size = "20"/>
<Script type = "text/javascript">
Function submit_form (){
// Javascript writing
Document. getElementById ("pname"). disabled = "";
Document. getElementById ("inputForm"). submit ();
// JQuery Writing Method
$ ("# Pname"). attr ("disabled", false );
$ ("# InputForm"). submit ();
}
</Script>
3. Get the pname value in js, pass it as a parameter, and modify the form action value. Of course, you need to modify the background code to get the parameter (getParameter );
Copy codeThe Code is 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
Var pname = document. getElementById ("pname"). value;
Document. getElementById ("pname"). action = "termrental. action? Panme = "+ pname +" & p = "+ new Date ();
Document. getElementById ("pname"). submit ();
}
</Script>
A little suggestion: When writing a script language, we are generally keen on jQuery writing because it is concise. jQuery encapsulates javascript and uses regular expressions to obtain the equivalent value of HTML,
Therefore, efficiency is bound to be affected. We recommend that you use javascript when there are a large number of scripts.

I hope this article will help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.