jquery Dynamic Change form attribute submit form _jquery

Source: Internet
Author: User
In some cases, the same form in different cases submitted to different processing actions, you can dynamically change the properties of the form in JS, meet the different conditions of form submission needs.

Such as:
Copy Code code 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= "updated to probe point" onclick= "Javascript:formsubmit ();" ></input>
</form>
<iframe name= "iframe" ></iframe>

Now requires condition 1 to be submitted in the way above, to method= "POST" enctype= "Multipart/form-data" action= "action1.jsp" target= "iframe" Submit to action1.jsp for processing, condition 2 to submit to action2.jsp processing in normal text, and open new page. You need to dynamically change the properties of the form through JS:
Copy Code code 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 changing the Enctype property of a form, write $ ("#form") only. attr ("Enctype", "application/x-www-form-urlencoded");
Will not work, the following two sentences must be combined to take effect:
Copy Code code as follows:

$ ("#form"). attr ("Enctype", "application/x-www-form-urlencoded");
$ ("#form"). attr ("Encoding", "application/x-www-form-urlencoded");

Where Enctype's attribute value meaning refers to the enctype properties of HTML <form> tags

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.