Can I get jquery.serializeArray () values ​​by key name?

I use the jquery.serializeArray () function and I send it to the server and everything works fine. However, I need to update a couple of client-side things that are serialized.

So instead of making another selector in the text box, I just want to extract it from the serialized array.

I'm not sure how to do this

Product=Test&Qty=50

So tell me if I have something like this. I can do something like this

 var sendFormData = form.serializeArray();
 var val = sendFormData["Product"].value;

but that doesn't seem to work. I can make it work when I do something like this

 var sendFormData = form.serializeArray();
 var val = sendFormData[0].value;

I really don't want to do this by index, as this means that if the order changes, all values ​​may be incorrect. If you could do this with a key name, that would not be a problem.

+3
1

, , , , , $.param() (.serialize() ), :

var arr = $("form").serializeArray();
$.each(arr, function(i, fd) {
    if(fd.name === "Product") fd.value = "New Value";
});    
var sendFormData = $.param(arr); //turn it into Product=Test&Qty=50 string format
//sendFormData == "Product=New+Value&Qty=50"

, , :

var sendFormData = $("form").serializeArray();
$.each(sendFormData , function(i, fd) {
    if(fd.name === "Product") fd.value = "New Value";
});
//sendFormData == "Product=New+Value&Qty=50"
+5

Source: https://habr.com/ru/post/1746896/


All Articles