jquery dynamically creates a form and submits examples

Source: Internet
Author: User

Sometimes when writing a Web application, you need to dynamically construct a form and submit it, the parameters and action in the form, the POST request or get request, and even the style of the form can be specified, which can certainly be done with native JavaScript, I did a test with jquery.

My own test is to construct a paging post request, in order to prevent the CSRF attack, joined the CSRF verification, do not need to be removed.

Example

Function gensearchobj (Url,page,pagesize,keyword) {    var params = {};
    params.url = url;
    params.page = page;
    params.pageSize = pageSize;
    params.cond = keyword;
    return params; } function mockformsubmit (params) {    var form = $ (' <form /> ',  {action : params.url, method: ' Post ',  style: ' Display:none; '}. Appendto (' body ');         $.each (Params, function (k, v)   {          if  ( k !=  "url"  ) {               form.append (' <input type = "hidden"  name= "'  + k + '" " value=" '  + v + ' "&nbsP;/> ');
          }     });     form.append (' <input type= "hidden"  name= "Csrftoken"  value= ""  + "
 $ ("#csrf_token"). Val ()  +  '  /> '  );
    form.submit (); }

Example 2

  <! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "http:// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

You need to add the created form to the DOM, and the browser renders it to respond to events and methods. Tmpform.appendto (document.body). Submit ();

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.