jQuery: serialize() forma i inne parametry

Czy możliwe jest wysłanie formularza elementów (serializowanych .serialize() metoda) i inne parametry z jednym żądaniem AJAX?

Przykład:

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

Jeśli nie, to jaki jest najlepszy sposób na przesłanie formularza wraz z innymi parametrami?

Thanks

Author: informatik01, 2012-05-01

9 answers

serialize() efektywnie zamienia wartości formularza w poprawny querystring, jako taki możesz po prostu dołączyć do ciągu znaków:

$.ajax({
    type : 'POST',
    url : 'url',
    data : $('#form').serialize() + "&par1=1&par2=2&par3=232"
}
 245
Author: Rory McCrossan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-01 14:04:08

Alternatywnie możesz użyć form.serialize() z $.param(object), jeśli przechowujesz swoje params w jakiejś zmiennej obiektowej. Użycie byłoby:

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

Zobacz http://api.jquery.com/jQuery.param w celach informacyjnych.

 80
Author: kliszaq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-11 19:32:39

Nie wiem, ale żadne z powyższych nie zadziałało na mnie, potem użyłem tego i zadziałało:

W tablicy seryjnej formularza jest ona zapisywana jako para wartości klucza

Wypchnęliśmy nową wartość lub wartości tutaj w zmiennej form i wtedy możemy przekazać tę zmienną bezpośrednio teraz.

var form = $('form.sigPad').serializeArray();
var uniquekey = {
      name: "uniquekey",
      value: $('#UniqueKey').val()
};
form.push(uniquekey);
 9
Author: Rohit Arora,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-15 08:54:36

Jeśli chcesz wysłać dane z formularzem serialize możesz spróbować tego

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);

    }
});
 1
Author: 4302836,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 08:17:51

Pass wartość parametru jak to

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

I tak dalej.

 1
Author: Sanjay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-20 05:20:40

Following answer, if you want to send an array of integer (almost for. NET), this is the code:

// ...

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)
,             

// ...
 0
Author: Dani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-28 14:02:47

Możesz również użyć funkcji serializeArray, aby zrobić to samo.

 -1
Author: Suresh Prajapat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-17 06:01:52

Możesz utworzyć formularz pomocniczy używając jQuery z zawartością innego formularza, a następnie dodać ten formularz inne paramy, więc musisz go serializować tylko w wywołaniu 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()
}
 -2
Author: Felixuko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-26 16:10:51

I fix the problem with under statement ; wyślij dane za pomocą adresu URL same get methode

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

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

 -2
Author: Aziz Fazli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-11 19:33:53