0

I need to serialize a form in a custom key/value pair format. This is how my HTML code looks like:

<form name="ltq_frm" id="ltq_frm">
  <table class="table" id="licenses_to_quote">
    <thead>
      <tr>
        <th>License ID</th>
        <th>Quote ID</th>
        <th>Agreement Type</th>
        <th>Customer Site</th>
      </tr>
    </thead>
    <tbody id="licenses_to_quote_body">
      <tr data-id="96">
        <td>
          96
          <input type="hidden" name="license_id" value="96">
        </td>
        <td>
          <input class="hidden-select2-96" type="hidden" name="quote_id" value="249"> 249
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
      <tr data-id="100">
        <td>
          100
          <input type="hidden" name="license_id" value="100">
        </td>
        <td>
          <input class="hidden-select2-100" type="hidden" name="quote_id" value="">
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
    </tbody>
  </table>
</form>

<button id="subm">
  Click
</button>

And this is how I am serializing it:

$(function() {
  $('#subm').click(function() {
    var sA = $('#ltq_frm').serializeArray();
    var s = $('#ltq_frm').serialize();

    console.log(sA);
    console.log(s);
  });
});

This is "working" but I am getting an array of four values as output. I would like to get something like:

[
    'license_id' => 'quote_id'
]

Using the example above:

[
    96  => 249,
    100 => 
]

The values above comes from the hidden elements on the form, those named as license_id and quote_id.

I was checking here but I couldn't get the idea in how to transform my values into the needed key/value pairs.

This is a jsFiddle I was working with in case you need it.

Note: this is a code in development so if you have a better suggestion or need to give me something different go ahead

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • I think you'd have to tell us where these values are supposed to come from. You want `96 => 249`, but `96` is in a data attribute, in a value and in part of a classname, does it matter where this value is gotten from etc? – adeneo Jun 15 '17 at 18:02
  • 1
    So like this then -> https://jsfiddle.net/kehxzq1L/3/ – adeneo Jun 15 '17 at 18:08
  • @adeneo yes sir, please add an answer as well I'll give you a vote ;) – ReynierPM Jun 15 '17 at 18:09

2 Answers2

1

I get it. You need a custom code like this to do it:

var finalObj = { };
$.each(​$('form')​.serializeArray()​, function() {
    finalObj[this.name] = this.value;
})​;

console.log(finalObj);

Snippet

var finalObj = { };
$.each($('form').serializeArray(), function() {
  finalObj[this.name] = this.value;
});

console.log(finalObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="ltq_frm" id="ltq_frm">
  <table class="table" id="licenses_to_quote">
    <thead>
      <tr>
        <th>License ID</th>
        <th>Quote ID</th>
        <th>Agreement Type</th>
        <th>Customer Site</th>
      </tr>
    </thead>
    <tbody id="licenses_to_quote_body">
      <tr data-id="96">
        <td>
          96
          <input type="hidden" name="license_id" value="96">
        </td>
        <td>
          <input class="hidden-select2-96" type="hidden" name="quote_id" value="249"> 249
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
      <tr data-id="100">
        <td>
          100
          <input type="hidden" name="license_id[]" value="100">
        </td>
        <td>
          <input class="hidden-select2-100" type="hidden" name="quote_id[]" value="">
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
    </tbody>
  </table>
</form>

Update: To get your custom way, I need to still change it further. In your question, the code has some inconsistencies. Those I corrected are as follows:

  • license_id[] to license_id.
  • quote_id[] to quote_id.

Please follow one single convention.

var finalObj = { };
var lastLID = "";
$.each($('form').serializeArray(), function() {
  if (this.name == "license_id")
    lastLID = this.value;
  else (this.name == "quote_id")
    finalObj[lastLID] = this.value;
});

console.log(finalObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="ltq_frm" id="ltq_frm">
  <table class="table" id="licenses_to_quote">
    <thead>
      <tr>
        <th>License ID</th>
        <th>Quote ID</th>
        <th>Agreement Type</th>
        <th>Customer Site</th>
      </tr>
    </thead>
    <tbody id="licenses_to_quote_body">
      <tr data-id="96">
        <td>
          96
          <input type="hidden" name="license_id" value="96">
        </td>
        <td>
          <input class="hidden-select2-96" type="hidden" name="quote_id" value="249"> 249
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
      <tr data-id="100">
        <td>
          100
          <input type="hidden" name="license_id" value="100">
        </td>
        <td>
          <input class="hidden-select2-100" type="hidden" name="quote_id" value="">
        </td>
        <td>Percentage Support</td>
        <td>Frito-Lay, Inc.</td>
      </tr>
    </tbody>
  </table>
</form>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

You could manually map each row, get the two inputs, use the first as the key and the second as the value, like this

var result = $('#ltq_frm tbody tr').map(function() {
    var inp = $('input', this), o   = {};

    o[inp.first().val()] = inp.last().val();
    return o;
}).get();

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388