jQuery在用ajax创建后从表中获得nth:child [英] jQuery get nth:child from table after creating that with ajax
问题描述
在这个下面的ajax我填写并获取数据后创建简单的数据
$。ajax({
method :'GET',
url:'/ analyzePage / searchTag /'+ tagName,
contentType:false,
processData:false,
success:function(data){
console.log(data);
setTimeout(function(){
if(data.state =='error'){
...
}
else {
let table ='< div class =table-responsive pre-scrollable>';
...
$ .each(data.tags,function(key,value){
table = table +'< tr class =searchTag-'+ value.name +'>';
table = table +'< td style =padding:6px 20px;> 1< / td>';
table = table +'< td style =padding:6px 0px;>';
table = table +'< ; img src ='+ value.profile_pic_url +'class =img-circle img-smid =tagIamge/>< / td>';
table = table +'< td style =padding:6px 20px;>'+ value.name +'< / td>';
table = table +'< td style =padding:6px 20px;>'+ value.media_count +'< / td>';
table = table +'< td>';
table = table +'< button type =buttonclass =btn btn-warning btn-icon btn-rounded legitRippleid =searchTag-'+ value.name +'>';
table = table +'< i class =icon-file-plus>< / i>';
table = table +'< / button>';
table = table +'< / td>';
table = table +'< / tr>';
});
...
$('。data')。html(table);
}
},800);
},
错误:function(data){
console.log(data);
}
});
因为我有一个按钮 id =searchTag-'+ value。它的名字+'
到最新的 td
中,我想获得这个按钮所有的列,比如 tr
有4 td
和最新 td
我有这个按钮,那么我想要在点击按钮后将其他 td
加入 tr
中
我的代码工作,但它的所有 tr
s到我的表中
$(document).on('click','[id ^ ='searchTag'],function(){
let thisId = $(this).attr(id);
let tagName = thisId.split( - )[1];
$([class ^ ='+ $(this).attr(id)+'] td:nth-child 3))。append(< span> Clicked< / span>);
});
是否要在图标旁边添加一个范围点击后它的按钮?
如果你这样做,你可以这样做:
<$ p $ (''c $ c> $(document).on('click','[id ^ ='searchTag'],function(){
$(this).closest('td')。 append(< span> Clicked< / span>);
});
我想要得到所有td哪个按钮进入此行
试试这个:
$(文件).on('click','[id ^ ='searchTag'],function(){
$(this).closest('tr')。find('td'); // return所有td元素
});
in this below ajax i fill and create simple data after getting data
$.ajax({
method: 'GET',
url: '/analyzePage/searchTag/' + tagName,
contentType: false,
processData: false,
success: function (data) {
console.log(data);
setTimeout(function () {
if (data.state == 'error') {
...
}
else {
let table = '<div class="table-responsive pre-scrollable">';
...
$.each(data.tags, function (key, value) {
table = table + '<tr class="searchTag-' + value.name + '">';
table = table + '<td style="padding: 6px 20px;">1</td>';
table = table + '<td style="padding: 6px 0px;">';
table = table + '<img src="' + value.profile_pic_url + '" class="img-circle img-sm" id="tagIamge"/></td>';
table = table + '<td style="padding: 6px 20px;">' + value.name + '</td>';
table = table + '<td style="padding: 6px 20px;">' + value.media_count + '</td>';
table = table + '<td>';
table = table + '<button type="button" class="btn btn-warning btn-icon btn-rounded legitRipple" id="searchTag-' + value.name + '">';
table = table + '<i class="icon-file-plus"></i>';
table = table + '</button>';
table = table + '</td>';
table = table + '</tr>';
});
...
$('.data').html(table);
}
}, 800);
},
error: function (data) {
console.log(data);
}
});
in that i have one button id="searchTag-' + value.name + '"
which its into latest td
i want to get all columns that this button is into that, for example this tr
have 4 td
and into latest td
i have this button, then i want to get other td
into this tr
after click on button
my code work but its get all tr
s into my table
$(document).on('click', "[id^='searchTag']", function () {
let thisId = $(this).attr("id");
let tagName = thisId.split("-")[1];
$("[class^='" + $(this).attr("id") + "'] td:nth-child(3)").append("<span>Clicked</span>");
});
Do you want to add a span next to the icon of the button after clicking to it?
If you do, you can do it like this:
$(document).on('click', "[id^='searchTag']", function () {
$(this).closest('td').append("<span>Clicked</span>");
});
i want to get all td which button is into this row
Try this:
$(document).on('click', "[id^='searchTag']", function () {
$(this).closest('tr').find('td'); // returns all "td" elements
});
这篇关于jQuery在用ajax创建后从表中获得nth:child的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!