我有一个按钮,当点击按钮时我需要增加按钮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/