简体   繁体   中英

Convert Form Data into Request Data for an AJAX Request

I am trying to submit my form data via a POST AJAX request and cannot find any solutions.

I can't just get the values by ID or name etc because it is dynamically created depending on data from a database.

I have tried using the childNodes and think this may be a solution but cannot figure it out. Do I need to use JQuery? Can it be done with just JS as I'm a beginner.

Any ideas would be appreciated, cheers.

Did you tried below

$( "form" ).submit(function( event ) {
 ( var jsonData = $( this ).serializeArray() );
  event.preventDefault();
 // --- Your Ajax request
});

What you can do is just give an id (here i am giving form_id ) to the form

$('#form_id').submit(function(){
  e.preventDefault();
  $.ajax({
        type: "POST",
        url: "your_url",
        data: $('#form_id').serialize(),
        success: function (data) {
            alert('ok');
        }
  });
})

Cheers. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM