简体   繁体   English

如何为使用jquery动态生成的按钮赋予唯一ID

[英]how to give unique id to buttons generated dynamically using jquery

<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> 

This is my table format. 这是我的表格格式。 I have used ajax and jquery mechanism to dynamically generate new rows where same buttons will be cloned. 我已经使用ajax和jquery机制来动态生成将克隆相同按钮的新行。 I want to generate a unique id to each button that is generated. 我想为每个生成的按钮生成唯一的ID。 Can anyone please help me on this task? 谁能帮我完成这个任务?

You can make use of variable i to generate button ids. 您可以使用变量i生成按钮ID。 Put some string for button id and append variable i as shown below 放置一些字符串作为按钮ID并附加变量i ,如下所示

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

So whole table look like 所以整个桌子看起来像

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM