Вик*_*ков 2 javascript ajax jquery
我找到了很多关于我的问题的答案,但问题没有解决
我有表格,有数据,例如:
<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>
Run Code Online (Sandbox Code Playgroud)
我需要:
对于此表中的每一行,获取当前行中 id="query" 和 id="foo" 链接的值并发送 ajax 查询
我尝试这样做:
$("#detect_rel").click(function(){
$('tr').each(function() {
// ajax to: ajax.php?query=xxxx&data=&xxxxx
});
});
Run Code Online (Sandbox Code Playgroud)
但我不能得到的内部文本<a id="query"...和<a id="foo"...两者的当前行
我试过这样的事情:
$('tr').each(function() {
var cols = $('.1, .2', this);
// do something with cols
});
Run Code Online (Sandbox Code Playgroud)
但这并没有帮助,因为我无法(mb 没有足够的 js 知识)获取当前行的内部文本<a id="query"...和<a id="foo"...两者
伪代码:
获取行
Run Code Online (Sandbox Code Playgroud)get value of link in column_one and column_two send ajax query获取下一行
等等
注意:<a href...在每个单元格中,“bootstrap x editable”需要,因为它,我需要在每个链接上使用 id
问题已解决,谢谢各位
在 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>
Run Code Online (Sandbox Code Playgroud)
你的 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
});
});
Run Code Online (Sandbox Code Playgroud)
我刚刚在 JSFiddle 上测试了这段代码,它可以工作。
演示:http : //jsfiddle.net/Pt2Vd/