jQuery:serialize() 表单和其他参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10398783/
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 09:14:49  来源:igfitidea点击:

jQuery: serialize() form and other parameters

jqueryajaxformsserialization

提问by KenavR

Is it possible to send formelements (serialized with .serialize()method) and other parameters with a single AJAX request?

是否可以使用单个 AJAX 请求发送表单元素(使用.serialize()方法序列化)和其他参数?

Example:

例子:

$.ajax({
    type : 'POST',
    url : 'url',
    data : {
        $('#form').serialize(),
        par1 : 1,
        par2 : '2',
        par3: 232
    }
}

If not what's the best way to submit a form together with other parameters?

如果不是,将表单与其他参数一起提交的最佳方式是什么?

Thanks

谢谢

回答by Rory McCrossan

serialize()effectively turns the form values into a valid querystring, as such you can simply append to the string:

serialize()有效地将表单值转换为有效的查询字符串,因此您可以简单地附加到字符串:

$.ajax({
    type : 'POST',
    url : 'url',
    data : $('#form').serialize() + "&par1=1&par2=2&par3=232"
}

回答by kliszaq

Alternatively you could use form.serialize()with $.param(object)if you store your params in some object variable. The usage would be:

或者form.serialize()$.param(object)如果您将参数存储在某个对象变量中,则可以使用with 。用法是:

var data = form.serialize() + '&' + $.param(object)

See http://api.jquery.com/jQuery.paramfor further reference.

请参阅http://api.jquery.com/jQuery.param以获取进一步参考。

回答by Rohit Arora

I dont know but none of the above worked for me, Then i used this and it worked :

我不知道,但以上都不适合我,然后我使用了它并且它起作用了:

In form's serialized array it is stored as key value pair

在表单的序列化数组中,它存储为键值对

We pushed the new value or values here in form variable and then we can pass this variable directly now.

我们在表单变量中推送了一个或多个新值,然后我们现在可以直接传递这个变量。

var form = $('form.sigPad').serializeArray();
var uniquekey = {
      name: "uniquekey",
      value: $('#UniqueKey').val()
};
form.push(uniquekey);

回答by 4302836

If you want to send data with form serialize you may try this

如果你想用表单序列化发送数据,你可以试试这个

var form= $("#formId");
$.ajax({
    type: form.attr('method'),
    url: form.attr('action'),
    data: form.serialize()+"&variable="+otherData,
    success: function (data) {
    var result=data;
    $('#result').attr("value",result);

    }
});

回答by Sanjay

pass value of parameter like this

像这样传递参数的值

data : $('#form_id').serialize() + "&parameter1=value1&parameter2=value2"

and so on.

等等。

回答by Dani

Following Rory McCrossananswer, if you want to send an arrayof integer (almost for .NET), this is the code:

按照Rory McCrossan 的回答,如果你想发送一个整数数组(几乎用于 .NET),这是代码:

// ...

url: "MyUrl",       //  For example --> @Url.Action("Method", "Controller")
method: "post",
traditional: true,  
data: 
    $('#myForm').serialize() +
    "&param1="xxx" +
    "&param2="33" +
    "&" + $.param({ paramArray: ["1","2","3"]}, true)
,             

// ...

回答by Suresh Prajapat

You can also use serializeArray function to do the same.

你也可以使用 serializeArray 函数来做同样的事情。

回答by Felixuko

You can create an auxiliar form using jQuery with the content of another form and then add thath form other params so you only have to serialize it in the ajax call.

您可以使用 jQuery 和另一个表单的内容创建一个辅助表单,然后将其他表单的内容添加到其他表单中,这样您只需在 ajax 调用中对其进行序列化。

function createInput(name,value){
    return $('<input>').attr({
        name: name,
        value: value
    });
}
$form = $("<form></form>");
$form.append($("#your_form input").clone());
$form.append(createInput('input_name', 'input_value'));
$form.append(createInput('input_name_2', 'input_value_2'));
....

$.ajax({
    type : 'POST',
    url : 'url',
    data : $form.serialize()
}

回答by Aziz Fazli

I fix the problem with under statement ; send data with url same GET methode

我用 under 语句解决了这个问题;使用 url 相同的 GET 方法发送数据

$.ajax({
    url: 'includes/get_ajax_function.php?value=Hyman&id='+id,
    type: 'post',
    data: $('#b-info1').serializeArray(),

and get value with $_REQUEST['value']OR $_GET['id']

并通过$_REQUEST['value']OR获得价值$_GET['id']