jQuery 如何像这样通过 $.ajax ( serialize() + extra data ) 添加数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4406348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:13:54  来源:igfitidea点击:

How to add data via $.ajax ( serialize() + extra data ) like this

jquery

提问by Joko Wandiro

I want to add extra data after i use $('#myForm').serialize() + extra data

我想在使用 $('#myForm').serialize() + extra data 后添加额外的数据

$.ajax({
   type: 'POST',
   url: $('#myForm').attr('action'),
   data: $('#myForm').serialize(),   // I WANT TO ADD EXTRA DATA + SERIALIZE DATA
   success: function(data){
      alert(data);
      $('.tampil_vr').text(data);
   }
});

回答by jthompson

What kind of data?

什么样的数据?

data: $('#myForm').serialize() + "&moredata=" + morevalue

The "data" parameter is just a URL encoded string. You can append to it however you like. See the API here.

“data”参数只是一个 URL 编码的字符串。您可以随心所欲地附加到它。请参阅此处的 API 。

回答by steadweb

Personally, I'd append the element to the form instead of hacking the serialized data, e.g.

就个人而言,我会将元素附加到表单中,而不是破解序列化数据,例如

moredata = 'your custom data here';

// do what you like with the input
$input = $('<input type="text" name="moredata"/>').val(morevalue);

// append to the form
$('#myForm').append($input);

// then..
data: $('#myForm').serialize()

That way, you don't have to worry about ?or &

这样,您不必担心?&

回答by PrakashG

You can do it like this:

你可以这样做:

postData[postData.length] = { name: "variable_name", value: variable_value };