加载DOM后,如何获取动态添加的最后一个单选按钮的ID
jQuery(document).on("click", ".create-wishlist-items li.wish-item", function() {
var thisItem = jQuery(this).index();
jQuery('#itoris-wishlist-radiostack div').eq(thisItem).children('.itoris-wishlist-radio').click();
jQuery('.itoris-wishlist-button-select').click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="itoris-wishlist-radiostack">
<div>
<input class="itoris-wishlist-radio" name="itoris-wishlist-radio" value="1020" id="itoris-wishlist-radio-1020" alt="Chezil" type="radio">
<label for="itoris-wishlist-radio-1020">Chezil</label>
</div>
<div>
<input class="itoris-wishlist-radio" name="itoris-wishlist-radio" value="1021" id="itoris-wishlist-radio-1021" alt="Chezil1" type="radio">
<label for="itoris-wishlist-radio-1021">Chezil1</label>
</div>
<div>
<input class="itoris-wishlist-radio" name="itoris-wishlist-radio" value="1019" id="itoris-wishlist-radio-1019" checked="checked" alt="Main" type="radio">
<label for="itoris-wishlist-radio-1019">Main</label>
</div>
</div>
最佳答案
您可以使用jQuery('.radio-wrap input').last().attr('id');
请参阅添加的代码段以供参考。
var c = 4;
function addRadio(){
jQuery('.radio-wrap').append('<input type="radio" id="'+ c +'" value="4">');
c++;
}
function getLast(){
var elID = jQuery('.radio-wrap input').last().attr('id');
console.log(elID);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" onclick="addRadio()">
Add radio
</button>
<div class="radio-wrap">
<input type="radio" id="1" value="1">
<input type="radio" id="2" value="2">
<input type="radio" id="3" value="3">
</div>
<button onclick="getLast()">Get Last</button>