javascript - 单击按钮时如何增加按钮ID

标签 javascript html yii2

我有一个按钮,当点击按钮时我需要增加按钮id 这是我的按钮

 <input type="button" id="repeataddon-1" name="repeataddon"
 class="repeataddon" value="add" >

脚本为

 $(document).on( "click",'.repeataddon',function( ) {
                     hoteladdons();
                     alert(this.id);
                 });

我可以将此代码用于我想将其 id 增加为相同的其他表属性

<table width="100%" class="book-table" >
    <tr>
        <th>Addon Name</th>
        <th>No of Addons</th>
        <th>Rate</th>
        <th>Addon Amount</th>

    </tr>
    <tr>
        <td>
            <article>
                <span>

          <?php echo $hoteladdon; ?>

              </span>
            </article>
        </td>
        <td>
            <article>
                             <span>
                    <select class="noofaddons" name="noofaddons" id="noofaddons-1">
                        <?php
                        for($i=0;$i<5;$i++){


                            echo "<option value='$i'>".$i."</option>";

                        }
                        ?>
                    </select>
                        </span>
            </article>
        </td>
        <td><label id="addonprice-msg-1"></label> </td>
        <td><label id="addontotal-msg-1"></label></td>

    </tr>
</table>

最佳答案

就像下面这样

$(document).ready(function() {
  $('.repeataddon').click(function() {
    // hoteladdons();
    var temp = $(this).attr('id').split('-');
    $(this).attr('id', temp[0] + '-' + (parseInt(temp[1]) + 1));
    console.log($(this).attr('id'));
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="repeataddon-1" name="repeataddon" class="repeataddon" value="add">

关于javascript - 单击按钮时如何增加按钮ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31849377/

相关文章:

php - 如何禁用 Yii 2 中 activeform 的客户端验证消息?

PHP Yii2密码加密

javascript - 有没有更简单的方法将多个类添加到不同的div jquery

Node : require vs constructor injection 中的 Javascript 依赖注入(inject)和 DIP

javascript - console.log(variableName) 正在打印 ID 为变量名的标签的内容,而不是我的变量

javascript - 为每个图像缩略图预览添加删除按钮

yii2 - 如何在前端和后端使用模型或模块

javascript - 如何在 Windows Metro Style 应用程序上重新加载 iframe 的内容

javascript - jQuery 源代码中正则表达式的澄清

javascript - 使用jquery获取其他域网站的源代码