Possible to get jquery .serializeArray() values out by key name?
I am using the jquery .serializeArray() function and I send it to the server and that works all good.However I need to update a couple things on the client side that are serialized.
So instead of doing another selector on the textbox I want to just开发者_如何学Go grab it out of the serialized array.
I am not sure how to do this
Product=Test&Qty=50
So say if I have something like this. I can I do something like this
var sendFormData = form.serializeArray();
var val = sendFormData["Product"].value;
but this seems not to work. I only can get it to work when I do something like this
var sendFormData = form.serializeArray();
var val = sendFormData[0].value;
I really don't want to do it by index since that means if the order changes the values could be all wrong. If you could do it by like keyname then that would not be a problem.
You can loop through the object, set what you want, then convert it to a parameter string if needed using $.param()
(.serialize()
does this internally), like this:
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"
You can see a demo here
If you don't need to serialize it to a string, just simplify it a bit, like this:
var sendFormData = $("form").serializeArray();
$.each(sendFormData , function(i, fd) {
if(fd.name === "Product") fd.value = "New Value";
});
//sendFormData == "Product=New+Value&Qty=50"
精彩评论