2012-04-07 46 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(); 
相關問題