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

這是我的表格格式。 我已經使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM