Giter VIP home page Giter VIP logo

Comments (3)

benlind avatar benlind commented on June 10, 2024 1

Thanks for the quick response.

Right, I forgot to include the invocation script:

$('form').serializeJSON({
  checkboxUncheckedValue: "0"
});

I agree that using numbered indices is the ideal solution, but with my setup it won't work that way. My inputs are all in table rows, and I want each table row to correspond to an entry in the rows[] array. Typically, numbering the rows explicitly would work, but I also need the rows to be able to be reordered, dynamically added, and dynamically removed. Then, when the rows are submitted to the server, I want to preserve the order of the rows at submission, not when the page was loaded. I don't want to have to re-index every single row whenever one is moved, added, or removed. So the best solution for my situation is to dynamically add new row[] indices by using row[] instead of row[$i], since the former will correctly index the rows at submission, while the latter only indexes them on page load, ignoring any dynamic changes.

I ended up just putting name="" on the checkboxes (making serializeJSON ignore them), and using jQuery when the form is submitted to map the checkboxes to the rows. It's definitely not ideal, but I can't find a serializeForm plugin that does what I need.

from jquery.serializejson.

marioizquierdo avatar marioizquierdo commented on June 10, 2024

Where are you getting those "0" from? How are you calling serializeJson from JavaScript and what options are you using?

In any case, having objects inside arrays is always problematic. The plugin can not know if you want to add a new element or modify the last element (since the name it's the same). The solution for this is to specify the array index to ensure they go to the right place:

<input type="checkbox" name="rows[0][replicate]" value="1" checked />
<input type="checkbox" name="rows[1][replicate]" value="1" checked />
<input type="checkbox" name="rows[2][replicate]" value="1" checked />

Now the plugin will understand those as a nested object

$('input').serializeJSON();
//=> {rows: {"0": {"replicate": "1"}, "1": {"replicate":  "1"}, "2": {"replicate": "1"}}

If you still need those elements to be an array, use the option useIntKeysAsArrayIndex:

$('input').serializeJSON({useIntKeysAsArrayIndex: true});
//=>
{
  rows:  [
    { replicate: "0" },
    { replicate: "1" },
    { replicate: "0" },
  ]
}

If you want to add "0" when unchecked, use the option checkboxUncheckedValue:

$('input[name="rows[1][replicate]"]').prop('checked', false)); // uncheck the checkbox in the middle

$('input').serializeJSON({useIntKeysAsArrayIndex: true, checkboxUncheckedValue: "0"});
//=> {rows: [{"replicate": "1"}, {"replicate":  "0"}, {"replicate": "1"}]}

But, instead of this all, I would probably just design inputs so they are W3C compatible, using plain unique property names and hidden inputs for unchecked values, for example:

<input type="hidden"     name="rows_to_replicate[0]" value="no" />
<input type="checkbox" name="rows_to_replicate[0]" value="yes" />

<input type="hidden"     name="rows_to_replicate[1]" value="no" />
<input type="checkbox" name="rows_to_replicate[1]" value="yes" checked />

<input type="hidden"     name="rows_to_replicate[2]" value="no" />
<input type="checkbox" name="rows_to_replicate[2]" value="yes" />
$('input').serializeJSON();
//=>
{
  rows_to_replicate:  [
    "0": "no",
    "1": "yes",
    "2": "no"
  ]
}

from jquery.serializejson.

marioizquierdo avatar marioizquierdo commented on June 10, 2024

Ok great.
A list of checkboxes is very tricky in HTML, because the default specification says to add the value to the form if checked, or add nothing if unchecked. Different serialization plugins might solve this problem in different ways, I opted for keeping standards as much as possible. Anyways, if your scenario is too non-standard, then it might be best to write your own jQuery code to get what you want, for example:

var serialized = [];
$('form input[type=checkbox]').each(function (i, el) {
  var $el = $(el);
  var replicated = $el.prop('checked') ? "1" : "0"
  serialized.push({replicate: replicated});
});
//=> serialized contains your checkboxes values

from jquery.serializejson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.