簡體   English   中英

無法使用 eq() 獲取動態添加的單選按鈕子項

[英]Not able to get the dynamically added radio button child with eq()

加載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">radio 1</label> </div> <div> <input class="itoris-wishlist-radio" name="itoris-wishlist-radio" value="1021" id="itoris-wishlist-radio-1021" alt="alt1" type="radio"> <label for="itoris-wishlist-radio-1021">radio 2</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">radio 3</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>

 //create new element (div) for #itoris-wishlist-radiostack var new_div=$('<div/>'); //create new radio for this div var new_input=$('<input/>'); new_input.attr({'type':'radio', 'class':'itoris-wishlist-radio','name':'itoris-wishlist-radio', 'value':'1022', 'id':'itoris-wishlist-radio-1022'}); //add raddio into div new_div.append(new_input); //create new label for this div var new_label=$('<label/>'); new_label.attr({'for':'itoris-wishlist-radio-1022'}).html('Test'); //add label into div new_div.append(new_label); //add new div into #itoris-wishlist-radiostack $('#itoris-wishlist-radiostack').append(new_div); //new radio button click!!! new_input.click();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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>

暫無
暫無

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

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