Jquery dynamically changes the form properties to submit the form

  • 2020-03-30 03:07:48
  • OfStack

In some cases, the same form is submitted to different processing actions under different circumstances. You can dynamically change the properties of the form in js to meet the requirements of the form submission under different conditions.

Such as:
 
<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> 

Now, if condition 1 is required, submit as above: method="POST" enctype="multipart/form-data" action="action1.jsp" target="iframe"; In the case of condition 2, you need to submit plain text to action2.jsp for processing and open a new page. Then, the properties of the form need to be changed dynamically through js:
 
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:

If you change the enctype attribute of the form, just write $("#form").attr("enctype","application/x-www-form-urlencoded");
Will not work, the following two sentences must be combined in order to work:
 
$("#form").attr("enctype","application/x-www-form-urlencoded"); 
$("#form").attr("encoding","application/x-www-form-urlencoded"); 

Where, the meaning of the attribute value of enctype refers to the blog post "HTML <form> tag's enctype attribute".

Related articles: