简体   繁体   English

将表单数据转换为AJAX请求的请求数据

[英]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. 我试图通过POST AJAX请求提交表单数据,但找不到任何解决方案。

I can't just get the values by ID or name etc because it is dynamically created depending on data from a database. 我不能只通过ID或名称等获取值,因为它是根据数据库中的数据动态创建的。

I have tried using the childNodes and think this may be a solution but cannot figure it out. 我已经尝试使用childNodes并认为这可能是一个解决方案,但无法弄明白。 Do I need to use JQuery? 我需要使用JQuery吗? Can it be done with just JS as I'm a beginner. 因为我是初学者,所以可以用JS完成。

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 你能做的就是给表单提供一个id(这里我给的是form_id

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

Cheers. 干杯。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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