2012-04-07 45 views
2

jQuery的空字符串serialize()从表单抛出空字符串,这里是形式jQuery的序列化()抛出在导轨

<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> 

的源极和JavaScript用来显示串行化输出。

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

有人能指出我在哪里做错了吗?

回答

3

添加 '#':

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

或者删除.id:寻找指定id表单时

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

它的工作,谢谢。 :) – 2012-04-07 09:32:21

0

你缺少#

但是,你能避免这个问题,只需使用.closest()找到祖先<form>

$(this).closest('form').serialize(); 
相关问题