Jquery dynamically changes form attributes to submit forms

Source: Internet
Author: User

In some cases, the same form can be submitted to different processing actions under different circumstances, and the attributes of the form can be dynamically changed in js to meet the requirements of form submission for different conditions.

For example:
Copy codeThe Code is as follows:
<Form id = "form" name = "form" method = "POST" enctype = "multipart/form-data" action = "action1.jsp" target = "iframe">
<Input type = "file" name = "file" id = "file" class = "input_text80"> </input>
<Input id = "name" name = "name"/>
<Input type = "button" value = "Update to probe point" onClick = "javascript: formSubmit ();"> </input>
</Form>
<Iframe name = "iframe"> </iframe>

If condition 1 is required, submit the statement in the preceding method, use method = "POST" enctype = "multipart/form-data" action = "action1.jsp" target = "iframe" to submit it to action1.jsp for processing; under Condition 2, you need to submit it to action2.jsp for processing in plain text mode and open a new page. You need to dynamically change the form attributes in js mode:
Copy codeThe Code is as follows:
Function formSubmit (){
If (flag = "1 "){
$ ("# Form"). submit ();
} Else if (flag = "2 "){
$ ("# Form"). attr ("action", "deployResult. jsp ");
$ ("# Form"). attr ("target", "_ blank ");
$ ("# Form"). attr ("method", "GET ");
$ ("# Form"). attr ("enctype", "application/x-www-form-urlencoded ");
$ ("# Form"). attr ("encoding", "application/x-www-form-urlencoded ");
$ ("# Form"). submit ();
}
}

Note:

When you change the enctype attribute of form, if you only write $ ("# form"). attr ("enctype", "application/x-www-form-urlencoded ");
It will not take effect. You must combine the following two sentences to make it take effect:
Copy codeThe Code is as follows:
$ ("# Form"). attr ("enctype", "application/x-www-form-urlencoded ");
$ ("# Form"). attr ("encoding", "application/x-www-form-urlencoded ");

For more information about the attribute values of enctype, see HTML <form> label's enctype attribute.

Related Article

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.