2010-05-13 49 views
0

如果我有這樣的代碼中的jquery:引用的形式的選擇在內部的jquery

var parentDiv = $(this).parent('.copyInstance'); 

此DIV有一種形式。

我該如何引用此選擇器中的表單元素?我想是這樣的

$.post($(parentDiv + " Form").attr('action'), $(parentDiv + " Form").serialize(), function(data) { 
    }); 

但是這上面似乎並沒有工作

+0

我不明白這個問題。 – cletus 2010-05-13 10:50:51

+0

@cletus - 我在試圖希望更好地解釋問題時添加了內容。讓我知道如果這有助於 – leora 2010-05-13 10:53:09

+0

也許這會更好,如果你解釋你想要做什麼以及爲什麼。 – cletus 2010-05-13 10:55:18

回答

5

要引用您可以使用.find()例如兒童元素:

$.post(parentDiv.find('form').attr('action'), 
     parentDiv.find('form').serialize(), 
    function(data) { 
    }); 

取決於它的結構你也可以使用.children()比如:

parentDiv.children('form') 
+0

'parentDiv'已經是一個jQuery對象,不需要克隆它:) – 2010-05-13 11:03:41

+0

我糾正了,我在考慮應該封裝在$()中的「this」,因爲它否則在某些IE版本中不起作用。 。 – alexteg 2010-05-13 15:29:01

2

您的這個選擇3個選項:

parentDiv.find('form') //finds any form at any level deep inside 
parentDiv.children('form') //finds forms that are immediate children 
$("form", parentDiv) //really calls .find() but easier on the eyes maybe? 

這裏有.find().children()jQuery(selector, context)更多信息。