Adding POST parameters before submit

前端 未结 7 912
情深已故
情深已故 2020-11-27 11:39

I\'ve this simple form:

7条回答
  •  日久生厌
    2020-11-27 11:57

    If you want to add parameters without modifying the form, you have to serialize the form, add your parameters and send it with AJAX:

    var formData = $("#commentForm").serializeArray();
    formData.push({name: "url", value: window.location.pathname});
    formData.push({name: "time", value: new Date().getTime()});
    
    $.post("api/comment", formData, function(data) {
      // request has finished, check for errors
      // and then for example redirect to another page
    });
    

    See .serializeArray() and $.post() documentation.

提交回复
热议问题