Javascript 获取要在ajax请求中传递的每个tr的值
我有这个表,我从数据库中获取数据。我想做的是从每个td的tr中获取数据,并将其传递到ajax请求中,以传递到另一个页面Javascript 获取要在ajax请求中传递的每个tr的值,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有这个表,我从数据库中获取数据。我想做的是从每个td的tr中获取数据,并将其传递到ajax请求中,以传递到另一个页面 while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { echo '<tr>'; echo '<td id="t_name">' .$row['name'
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '<tr>';
echo '<td id="t_name">' .$row['name']. '<input type="hidden" id="sell_id" value='.$row['id'].'> <input type="hidden" id="t_id" value='.$row['telco_id'].' > </td>';
echo '<td id="keyword_name">' .$row['keyword']. ' <input type="hidden" id="keyword_id" value='.$row['keyword_id'].' > </td>';
echo '<td id="main_cross">' .$row['main_message']. '</td>';
echo '<td id="alternate_cross">' .$row['alternate_message']. '</td>';
echo '<td id="start_date">' .$row['start_timestamp'].' </td>';
echo '<td id="end_date">' .$row['end_timestamp'].' </td>';
echo '<td> <a href="javascript:void(0);" id="editCrossSell"> <i class="menu-icon icon-edit" style="color:black;"></i></a> <a href="javascript:void(0);"> <i class="menu-icon icon-trash" style="color:black;"> </i> </a> </td>';
echo '</tr>';
$count++;
}
将id=“editCrossSell”更改为class=“editCrossSell”,将$(“#editCrossSell”)更改为$(“.editCrossSell”)
或
向每个editCrossSell ID添加一个索引,如editCrossSell 1、editCrossSell 2等(通过在循环中定义一个索引并在每个循环中增加一个索引)将ID=“editCrossSell”更改为class=“editCrossSell”,并将$('#editCrossSell')更改为$('.editCrossSell')
或
向每个editCrossSell ID添加一个索引,如editCrossSell 1、editCrossSell 2等(通过在循环中定义一个索引,并在每个循环中增加一个索引)首先,如果生成了多个
tds
,则应选择class
属性,如下所示
echo '<tr>';
echo '<td class="t_name">' .$row['name']. '<input type="hidden" id="sell_id" value='.$row['id'].'> <input type="hidden" id="t_id" value='.$row['telco_id'].' > </td>';
echo '<td class="keyword_name">' .$row['keyword']. ' <input type="hidden" id="keyword_id" value='.$row['keyword_id'].' > </td>';
echo '<td class="main_cross">' .$row['main_message']. '</td>';
echo '<td class="alternate_cross">' .$row['alternate_message']. '</td>';
echo '<td class="start_date">' .$row['start_timestamp'].' </td>';
echo '<td class="end_date">' .$row['end_timestamp'].' </td>';
echo '<td> <a href="javascript:void(0);" id="editCrossSell"> <i class="menu-icon icon-edit" style="color:black;"></i></a> <a href="javascript:void(0);"> <i class="menu-icon icon-trash" style="color:black;"> </i> </a> </td>';
echo '</tr>';
相应地更改代码。首先,如果正在生成多个
tds
,则应选择class
属性,如下所示
echo '<tr>';
echo '<td class="t_name">' .$row['name']. '<input type="hidden" id="sell_id" value='.$row['id'].'> <input type="hidden" id="t_id" value='.$row['telco_id'].' > </td>';
echo '<td class="keyword_name">' .$row['keyword']. ' <input type="hidden" id="keyword_id" value='.$row['keyword_id'].' > </td>';
echo '<td class="main_cross">' .$row['main_message']. '</td>';
echo '<td class="alternate_cross">' .$row['alternate_message']. '</td>';
echo '<td class="start_date">' .$row['start_timestamp'].' </td>';
echo '<td class="end_date">' .$row['end_timestamp'].' </td>';
echo '<td> <a href="javascript:void(0);" id="editCrossSell"> <i class="menu-icon icon-edit" style="color:black;"></i></a> <a href="javascript:void(0);"> <i class="menu-icon icon-trash" style="color:black;"> </i> </a> </td>';
echo '</tr>';
$('.editCrossSell').click(function() {// change id to class
var tablerow = $(this).closest('tr');
var t_name = tablerow.find('.t_name').text();//change to class
var keyword_name = tablerow.find('.keyword_name').text();//change to class
var t_id = tablerow.find('.tid').val();//change to class
var keyword_id = tablerow.find('.keyword_id').val();//change to class
var cross_id = tablerow.find('.sell_id').val();//change to class
var main_sell = tablerow.find('.main_cross').text();//change to class
var alteernate_sell = tablerow.find('.alternate_cross').text();//change to class
var start_date = tablerow.find('.#start_date').text();//change to class
var end_date = tablerow.find('.end_date').text();//change to class
$.ajax({
type: "POST",
url: "editCrossSell.php",
async: true,
data: {
t_Name: t_name,
keyName: keyword_name,
t_Id: t_id,
keyId: keyword_id,
main_cross: main_sell,
alternate_cross: alteernate_sell,
startDate: start_date,
endDate: end_date,
action: "update",
cross_sell_id: cross_id
},
success: function(data) {
$('#module').html(data);
$('#moduleSearch').hide();
},
beforeSend: function() {
$('#loader').show();
},
complete: function() {
$('#loader').hide();
}
});
});
相应地更改代码
$('.editCrossSell').click(function() {// change id to class
var tablerow = $(this).closest('tr');
var t_name = tablerow.find('.t_name').text();//change to class
var keyword_name = tablerow.find('.keyword_name').text();//change to class
var t_id = tablerow.find('.tid').val();//change to class
var keyword_id = tablerow.find('.keyword_id').val();//change to class
var cross_id = tablerow.find('.sell_id').val();//change to class
var main_sell = tablerow.find('.main_cross').text();//change to class
var alteernate_sell = tablerow.find('.alternate_cross').text();//change to class
var start_date = tablerow.find('.#start_date').text();//change to class
var end_date = tablerow.find('.end_date').text();//change to class
$.ajax({
type: "POST",
url: "editCrossSell.php",
async: true,
data: {
t_Name: t_name,
keyName: keyword_name,
t_Id: t_id,
keyId: keyword_id,
main_cross: main_sell,
alternate_cross: alteernate_sell,
startDate: start_date,
endDate: end_date,
action: "update",
cross_sell_id: cross_id
},
success: function(data) {
$('#module').html(data);
$('#moduleSearch').hide();
},
beforeSend: function() {
$('#loader').show();
},
complete: function() {
$('#loader').hide();
}
});
});
这样试试。我希望我理解正确
这样试试。我希望我正确地理解了你把它放在
中。每个循环
比如说$('#tableid tr')。每个(function(){//your code here var t_name=$(this)。find('.t_name').text();//change id to class})
但是你需要将id
更改为classwill。单击可以指定每一行吗?它将获得所述表的每一行$('#crossTable tr')。每个(函数(){$('#editCrossSell')。单击(函数(){});我这样尝试过。但是它只会再次获得第一行什么是editCrossSell
?tr?将它放在中。每个循环
说例如$('#tableid tr')。每个(函数(){//您的代码在这里var t\u name=$(这个)。查找('.t\u name')。text();//将id更改为class}
但您需要将id
更改为类。单击可以指定每一行吗?它将获得所述表$('#crossTable tr')的每一行。每一行(函数(){$('#editCrossSell')。单击(函数(){});我试过这样做。但是它只得到了第一行什么是editCrossSell
?tr?$('#editCrossSell')。单击(function(){
应该是$('.editCrossSell')。单击(function(){
更改类的ID
ID应该总是唯一的$('#editCrossSell')。单击(function(){/code>应该是$('.editCrossSell')。单击(function(){
changeID to class
ID应始终是唯一的