2013-07-09 20 views
2

我發現我的問題很多答案,但問題沒有得到解決JQuery。發送AJAX查詢的每一行表

我有表,使用數據,例如:

<table> 
    <tr> 
     <td class="editable"> <a id="query" href="#"> Data 1 </a> </td> 
     <td class="editable"> <a id="text" href="#"> Data 2 </a> </td> 
     <td class="editable"> <a id="foo" href="#"> Data 3 </a> </td> 
     <td class="editable"> <a id="bar" href="#"> Data 4 </a> </td> 
    </tr> 
    <tr> 
     <td class="editable"> <a id="query" href="#"> Data 1 </a> </td> 
     <td class="editable"> <a id="text" href="#"> Data 2 </a> </td> 
     <td class="editable"> <a id="foo" href="#"> Data 3 </a> </td> 
     <td class="editable"> <a id="bar" href="#"> Data 4 </a> </td> 
    </tr> 
</table> 

我需要:

有關本表中的每一行,當前行中得到使用id =「查詢」和id =「foo」的鏈接值併發送AJAX查詢

我嘗試這樣做:

$("#detect_rel").click(function(){ 
     $('tr').each(function() { 
      // ajax to: ajax.php?query=xxxx&data=&xxxxx 
     }); 
    }); 

,但我不能得到的<a id="query"...<a id="foo"...兩個內部文本,對當前行

我想是這樣的:

$('tr').each(function() { 
    var cols = $('.1, .2', this); 
    // do something with cols 
}); 

但它好好嘗試一下幫助,因爲我不能(MB不足夠JS知識)獲得的<a id="query"...<a id="foo"...兩個內部文本,對當前行

僞代碼:

GET行

get value of link in column_one and column_two 

send ajax query 

獲得下一行

注:<a href...在每個單元中,需要 '引導X編輯',和曲子的話,我需要使用ID在每個鏈接處


PR OBLEM解決,謝謝你們

+0

要知道,你的HTML是無效的; ID在整個頁面中都是唯一的。考慮將它們轉換爲類。 –

+0

是的,我知道,它的「bootstrap x editable」因爲他使用「id」作爲元素的「名稱」,就像

回答

4

在HTML ID中必須是唯一的,而類可以在頁面中多次出現。要做到你的要求,你的HTML應該是這樣的:

<table> 
    <tr> 
     <td class="editable"> <a class="query" href="#"> Data 1 </a> </td> 
     <td class="editable"> <a class="text" href="#"> Data 2 </a> </td> 
     <td class="editable"> <a class="foo" href="#"> Data 3 </a> </td> 
     <td class="editable"> <a class="bar" href="#"> Data 4 </a> </td> 
    </tr> 
    <tr> 
     <td class="editable"> <a class="query" href="#"> Data 1 </a> </td> 
     <td class="editable"> <a class="text" href="#"> Data 2 </a> </td> 
     <td class="editable"> <a class="foo" href="#"> Data 3 </a> </td> 
     <td class="editable"> <a class="bar" href="#"> Data 4 </a> </td> 
    </tr> 
</table> 

而jQuery代碼應該做這樣的事情:

$('#detect_rel').click(function() { 
    $('tr').each(function(i, el) { 
     var query = $(el).children('td').children('.query').text(); 
     var text = $(el).children('td').children('.text').text(); 
     //$.ajax (do your AJAX call here using values of query and text 
    }); 
}); 

我剛纔測試了這個的jsfiddle代碼和它的作品。

演示:http://jsfiddle.net/Pt2Vd/

+1

是的,這段代碼在工作。真的很大thx;) 但我有點糾正它: $(el).children('。editable')。children('。query')。文本(); 只有在這之後,它的工作 –

+0

我只是做了一些編輯,並在JSFiddle上測試它。讓我知道事情的後續! – jrthib

+0

是的,它的工作非常完美 –