英文:
Selecting a dynamically generated string
问题
On button click, I want to send data to the server for a certain line. Rows in the table are created dynamically
There is the following code:
<tr class="ordertr table-bordered">
<td id="recid" class="d-none"><?=$row['RecId']?></td>
<td class="ps-lg-3">
<ul>
<li><button id="editingST">✎</button></li>
<li><button class="towork">🛠</button></li>
</ul>
</td>
</tr>
And JS:
$("#position-order tbody").on('click', '.towork', function gotowork(){
$.ajax({
type: "POST",
url: '/towork.php',
data: {recid: $('#recid').text()}
});
return false;
});
recid
is defined, but for all rows it takes the value from the first row
I think there is an error here - $("#position-order tbody")
英文:
On button click, I want to send data to the server for a certain line. Rows in the table are created dynamically
There is the following code:
<tr class="ordertr table-bordered">
<td id="recid" class="d-none"><?=$row['RecId']?></td>
<td class="ps-lg-3">
<ul>
<li><button id="editingST">&#9998;</button></li>
<li><button class="towork">🛠</button></li>
</ul>
</td>
</tr>
And JS:
$("#position-order tbody").on('click', '.towork', function gotowork(){
$.ajax({
type: "POST",
url: '/towork.php',
data: {recid: $('#recid').text()}
});
return false;
});
recid
is defined, but for all rows it takes the value from the first row
I think there is an error here - $("#position-order tbody")
答案1
得分: 0
你可以获取包含按钮的行,然后查询该特定的 td。
function gotowork(e) {
const recid = $(e.target).closest('tr').find('#recid').text();
// 进行请求...
}
但是,在文档中,id 应该是唯一的,所以你应该使用类(class)来代替。
<td class="recid d-none">文本...</td>
$(e.target).closest('tr').find('.recid').text()
英文:
You can get the row containing the button, then query for that specific td.
function gotowork(e) {
const recid = $(e.target).closest('tr').find('#recid').text();
// make request...
}
However, ids should be unique in a document, so you should use a class for this instead.
<td class="recid d-none">text...</td>
$(e.target).closest('tr').find('.recid').text()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论