2

jQuery serialize() throws empty string from a form, here is the source of the form

<form accept-charset="UTF-8" action="/asdf" id="form_for_asdf_1_option_1" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="asdfaaqwefasdfwefefsefefwew=" /></div>
    <input name="checkbox_update_vote[asdf_id]" type="hidden" value="0" /><input id="dbr_1_of_asdf_1" name="checkbox_update_vote[asdf_id]" type="checkbox" value="1" />
    <input id="option_1_of_asdf_1" name="checkbox_update_vote[option_id]" type="hidden" value="1" />
    <input id="del_1_of_asdf_1" name="checkbox_update_vote[del]" type="hidden" value="false" />
    <br />
</form>

and the JavaScript used to show the serialized output.

$(document).ready(function() {
  $('input[type="checkbox"]').change(function() {
    alert($($(this).parents("form")[0].id).serialize());
  });
});

can some one point me where I am doing mistake?

2 Answers 2

3

Add '#':

$("#" + $(this).parents("form")[0].id)

Or remove .id:

$($(this).parents("form")[0])
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing the # when looking for the form with the given id.

However, you could avoid this problem and simply use .closest() to find the ancestor <form>

$(this).closest('form').serialize();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.