gpt4 book ai didi

javascript - 如何为使用jquery动态生成的按钮提供唯一的id

转载 作者:行者123 更新时间:2023-11-28 00:33:25 24 4
gpt4 key购买 nike

<table border="0" class="tableDemo bordered">
<tr class="ajaxTitle">
<th width="2%">Sr</th>
<th >SM</th>
<th >Campaign</th>
<th >Day1</th>
<th >Day2</th>
<th >Day3</th>
<th >Day4</th>
<th >Day5</th>
<th>Day6</th>
<th >Day7</th>
<th>Action</th>
</tr>
<?php
if(count($records)){
$i = 1;
foreach($records as $key=>$eachRecord){
?>
<tr id="<?php echo$eachRecord['id']; ?>">
<td><?php $i++; ?></td>
<td class="sm"><?php echo $eachRecord['sm'];?></td>
<td class="campaign"><?php echo $eachRecord['campaign'];?></td>
<td class="day1"><?php echo $eachRecord['day1'];?>
<button onclick="myFunction()">view</button></td>

<td class="day2"><?php echo $eachRecord['day2'];?>
<button onclick="myFunction()">view</button></td>

<td class="day3"><?php echo $eachRecord['day3'];?>
<button onclick="myFunction()">view</button> </td>

<td class="day4"><?php echo $eachRecord['day4'];?>
<button onclick="myFunction()">view</button></td>

<td class="day5"><?php echo $eachRecord['day5'];?>
<button onclick="myFunction()">view</button> </td>

<td class="day6"><?php echo $eachRecord['day6'];?>
<button onclick="myFunction()">view</button> </td>

<td class="day7"><?php echo $eachRecord['day7'];?>
<button onclick="myFunction()">view</button> </td>
<td>
<a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxEdit"><img src="" class="eimage"></a>
<a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxDelete"><img src="" class="dimage"></a>
</td>
</tr>
<?php }
}
?>
</table>

这是我的表格格式。我使用 ajax 和 jquery 机制来动态生成新行,其中将克隆相同的按钮。我想为生成的每个按钮生成一个唯一的 id。谁能帮我完成这项任务吗?

最佳答案

您可以使用变量i来生成按钮ID。为按钮 id 添加一些字符串并附加变量 i 如下所示

<button onclick="myFunction()" id="firstBtn<?php $i; ?>">view</button></td>

所以整个表格看起来像

<table border="0" class="tableDemo bordered">
<tr class="ajaxTitle">
<th width="2%">Sr</th>
<th >SM</th>
<th >Campaign</th>
<th >Day1</th>
<th >Day2</th>
<th >Day3</th>
<th >Day4</th>
<th >Day5</th>
<th>Day6</th>
<th >Day7</th>
<th>Action</th>
</tr>
<?php
if(count($records)){
$i = 1;
foreach($records as $key=>$eachRecord){
?>
<tr id="<?php echo$eachRecord['id']; ?>">
<td><?php $i++; ?></td>
<td class="sm"><?php echo $eachRecord['sm'];?></td>
<td class="campaign"><?php echo $eachRecord['campaign'];?></td>
<td class="day1"><?php echo $eachRecord['day1'];?>
<button onclick="myFunction()" id="firstBtn<?php $i; ?>">view</button></td>

<td class="day2"><?php echo $eachRecord['day2'];?>
<button onclick="myFunction()" id="secondBtn<?php $i; ?>">view</button></td>

<td class="day3"><?php echo $eachRecord['day3'];?>
<button onclick="myFunction()" id="thirdBtn<?php $i; ?>">view</button> </td>

<td class="day4"><?php echo $eachRecord['day4'];?>
<button onclick="myFunction()" id="forthBtn<?php $i; ?>">view</button></td>

<td class="day5"><?php echo $eachRecord['day5'];?>
<button onclick="myFunction()" id="fiftBtn<?php $i; ?>">view</button> </td>

<td class="day6"><?php echo $eachRecord['day6'];?>
<button onclick="myFunction()" id="sixthBtn<?php $i; ?>">view</button> </td>

<td class="day7"><?php echo $eachRecord['day7'];?>
<button onclick="myFunction()" id="seventhBtn<?php $i; ?>">view</button> </td>
<td>
<a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxEdit"><img src="" class="eimage"></a>
<a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxDelete"><img src="" class="dimage"></a>
</td>
</tr>
<?php }
}
?>
</table>

关于javascript - 如何为使用jquery动态生成的按钮提供唯一的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28738046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com