2015-11-08 59 views
0

這裏的情況:MooTools的:從點擊鏈接的兄弟表節點文本

| ID | Name | ... 
+----+--------+--- 
| 12 | Henry | ...  a whole list of names, ids,&c ...you get the idea 
+----+--------+--- 
| 13 | Julia | ... 
+-------------+--- 
... 

所有的名字都鏈接。選中時,它們通過將名稱傳遞給快速mysql查詢來加載表的其餘部分以進行編輯。但由於我得到了愚蠢的消息,我需要抓住身份證以傳遞。我還需要它來更新更正(和正確!)記錄。

所以,使用MooTools,當我點擊鏈接時,如何從鄰居兄弟中獲取文本?地獄,甚至只是普通的'JS會做的。

這裏的現有功能:

function loadRecord(aName) { 
    console.log("loadRecord called with: "+aName); 
    user = $('submitterName').value; 
    id = $('table-id').text; // THIS IS THE PROBLEM RIGHT HERE. >_< 
    console.log('with user: '+user); 
    $('recordName').value = aName; 
    $('addRecordButton').value = 'Update Record'; 
    $('addRecordData').action='_php/updateRecord.php'; // this isn't working either... 
    var action=$('addRecordData').action; 
    console.log(action); 
    checkUserDB(aName,user,id); 
} 

,因爲有多個行的表,我無法唯一標識符分配給每個ID 任何益處。我仍然試圖拉取與點擊鏈接相關的表格ID的文字。

我希望我很清楚。任何幫助或照明將不勝感激。

TIA船員!

WR!

+0

筆可以粘貼你的HTML的一個例子嗎?並解釋你認爲'$('table-id')。text'的作用?那個字符串總是一樣的嗎?你爲什麼不使用MooTools的get('text')',爲什麼不在像'data-'字段那樣點擊的鏈接中添加你想要的ID? – Sergio

+0

$('table-id).text返回元素中的文本。不知道如何實現moo-data插件,或者我會使用數據字段。 – WhiteRau

回答

0

下面是解

HTML

<!--Change your HTML--> 
<table> 
    <tr> 
    <th>ID</th> 
    <th>Name</th> 
    </tr> 
    <tr> 
    <td>12</td> 
    <td><a>Henry</a></td> 
    </tr> 
    <tr> 
    <td>13</td> 
    <td><a>Julia</a></td> 
    </tr> 
</table> 

MooTools

window.addEvent('load', function(){ 
    window.addEvent('click:relay(a)', function(event){//Add your selector inside relay 
    event.preventDefault(); 
    console.log($(this).getParent('td').getPrevious().get('html')); 
    //Add Your AJAX code here 
    }); 
}); 

這裏要連接一個click event錨然後selecting parenthttp://mootools.net/core/docs/1.5.2/Element/Element#Element:getParent) 然後當你在previous elementhttp://mootools.net/core/docs/1.5.2/Element/Element#Element:getPrevious) 可以有ID grab the previous element and get it's HTML

您可以點擊這裏http://codepen.io/arifmahmudrana/pen/yYQeJX

+0

優秀!我有結構,感謝代碼。你解釋得非常清楚,我非常感謝,因爲我明白解決方案。 – WhiteRau

+0

@WhiteRau謝謝,然後請upvote&選擇它作爲正確的答案。 –

相關問題