作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<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/
我是一名优秀的程序员,十分优秀!