JS get page data to perform AJAX requests

Source: Internet
Author: User

The following example shows how to use JS to get the values of the elements in the page and execute the AJAX requests as parameters.

$ ("#submit-task"). Bind ("click", Function (event) {Event.preventdefault ();    Event.stoppropagation ();    if (validate () = = False) {exit;    } var submitbtn = this;    Prohibit Submit button $ (submitbtn). attr (' disabled ', ' disabled ');    var formData = new FormData ($ (' #task-form ') [0]);    var task_id = $ ("input#task_id"). Val ();    var type = "PUT";    var url = '/api/v1/task/task/update/' +task_id; $.ajax ({type:type, Url:url, Data:formdata, Contenttype:false, C Ache:false, Async:false, Processdata:false, success:function (data) {Laye                R.msg ("Task Creation success");            Success after jumping to the homepage window.location.href = "/task/list";                }, Error:function (err) {Console.log (err);                Layer.msg ("Submission failed, reason for failure:" + err.responsetext);            Make the Submit button re-effective $ (SUBMITBTN). Removeattr (' disabled '); }        });}); Function VAlidate () {var fields = {' plan_name ': ' Plan name ', ' product_url ': ' Product link ', ' shop_name ': ' Shop name ',    ' shop_id ': ' Store id ', ' product_id ': ' Product ID ',};        for (Var fiels in fields) {var obj = document.getelementsbyname (fiels) [0];            if (Obj.value = = NULL | | obj.value = = "") {Obj.focus ();            Alert (Fields[fiels] + "cannot be empty");        return false; }} return true;
    • Get values for all elements in a form

      var formData = new FormData($('#task-form')[0]);

      The task-form is the ID value of the form tag, and it is worth noting that getting the form data is obtained from the element's Name property, which, when fetched in the background, is similar to the value of the Name property as the key, and the Value property is treated as a value.

    • Get the value of a single element

      var task_id = $("input#task_id").val();var search = $("input[name='search']").val();var username = $("#register-form input[name='username']").val();var obj = document.getElementsByName('name')[0].value;
    • The above data is in form format, or it can be used in JSON format

      // 构造参数var data = {task: 'task',action: 'action',times: 'times'}
      // 配置ajax参数data: JSON.stringify(data),contentType: "application/json",

JS get page data to perform AJAX requests

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.