2016-03-20 52 views
2

我想簡單地添加一個row表後接row當後者被點擊,所以我用下面的代碼(實際上我在Visualforce頁面中使用此代碼)。parent()和nearest()如何在jQuery中工作?

function queryAndUpdateLead() { 
     var a = $('[id$=ibrahim]').closest('tr') 
       .after('<tr><td>New Content 1</td><td>\ 
         <a href="#">New Content 2</a></td></tr>'); 
} 

這裏的問題是,這列在表中所有現有行後添加,當我用length屬性來檢查元素的數量a我總是得到2(我最初2行)

alert('a.length') // this shows 2 

我想要的是得到直接父行,而不是所有的現有行,但我不知道爲什麼它不工作?

編輯:html代碼提煉BY SALESFORCE SERVER:

<tbody id="j_id0:j_id3:j_id4:j_id6:tb"> 
<tr class="dataRow even first" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" onmouseout="if (window.hiOff){hiOff(this);} " onmouseover="if (window.hiOn){hiOn(this);} "> 
**<td id="j_id0:j_id3:j_id4:j_id6:0:ibrahim" class="dataCell " onclick="queryAndUpdateLead();" colspan="1">** 
<td id="j_id0:j_id3:j_id4:j_id6:0:j_id7" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:0:j_id8" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:0:j_id9" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:0:j_id10" class="dataCell " colspan="1"> 
</tr> 
<tr class="dataRow odd last" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" onmouseout="if (window.hiOff){hiOff(this);} " onmouseover="if (window.hiOn){hiOn(this);} "> 
<td id="j_id0:j_id3:j_id4:j_id6:1:ibrahim" class="dataCell " onclick="queryAndUpdateLead();" colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:1:j_id7" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:1:j_id8" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:1:j_id9" class="dataCell " colspan="1"> 
<td id="j_id0:j_id3:j_id4:j_id6:1:j_id10" class="dataCell " colspan="1"> 
</tr> 
</tbody> 
</table> 
+0

是否所有的行都有以「ibrahim」結尾的ID?因爲那是你使用的選擇器。 –

+0

不,只有一行被命名爲易卜拉欣 –

+1

然後請顯示您的HTML;和足夠的jQuery來重現你的問題,否則我們只能玩猜謎遊戲。 –

回答

0

當你有$('[id$=ibrahim]')元素的集合,你需要使用$(this)找到最接近當前點擊的元素的tr後插入內容:

function queryAndUpdateLead() { 
     var $(this).closest('tr') 
       .after('<tr><td>New Content 1</td><td>\ 
         <a href="#">New Content 2</a></td></tr>'); 
} 
+0

是的,它的工作表示感謝。 –