Javascript 如何为使用jquery动态生成的按钮提供唯一id Sr 性虐待 运动 第1天 第2天 第三天 第4天 第5天 第6天 第7天 行动 看法 看法 看法 看法 看法 看法 看法
这是我的表格格式。我使用ajax和jquery机制动态生成新行,在这些新行中,相同的按钮将被克隆。我想为生成的每个按钮生成一个唯一的id。有人能帮我完成这项任务吗?您可以使用变量Javascript 如何为使用jquery动态生成的按钮提供唯一id Sr 性虐待 运动 第1天 第2天 第三天 第4天 第5天 第6天 第7天 行动 看法 看法 看法 看法 看法 看法 看法,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我的表格格式。我使用ajax和jquery机制动态生成新行,在这些新行中,相同的按钮将被克隆。我想为生成的每个按钮生成一个唯一的id。有人能帮我完成这项任务吗?您可以使用变量i生成按钮ID。为按钮id放入一些字符串并附加变量i,如下所示 <table border="0" class="tableDemo bordered"> <tr class="ajaxTitle"> <th width="2%">Sr</th>
i
生成按钮ID。为按钮id放入一些字符串并附加变量i
,如下所示
<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>
@杰德帕特尔·杜德。。唯一ID的意思是它不一样。。。。所以他的问题是合法的…谢谢你,伙计…让我把我的问题说清楚…正如你所看到的,我在每列中有7列一个按钮和文本框…我可以使用id属性给前7个按钮提供id…但是当动态生成新行时,新的7个按钮将是前7个按钮的副本…我想为这些按钮生成唯一的id还有按钮…谢谢你..@Dennis,@jd Pateltank你..@Bhushan Kawadkar的答案…但我希望为动态生成的按钮生成唯一或增量id..这将生成唯一和增量id,就像你在firsttd
中所做的那样,因此你将获得类似firstBtn1、firstBtn2、,firstBtn3…我提供的代码将仅为按钮创建id,而不是为行创建id。请帮助我如何在单击按钮时将这些id存储在mysql数据库中。。
<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>