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

时间:2015-02-26 08:56:36

标签: javascript jquery ajax

<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。有人可以帮我完成这项任务吗?

1 个答案:

答案 0 :(得分:1)

您可以使用变量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>