簡體   English   中英

如何確定使用jQuery從動態創建的列表中選擇了哪個項目

[英]How do I figure out which item was selected from a dynamically created list using jQuery

當頁面加載時,我正在向ul動態添加項目。

addUsersToDropdown = (user_markers) ->
  console.log("inside addUsersToDropdown")
  for marker in user_markers
    console.log("adding name to dropdown , name = #{marker.name}")
    jQuery("#user_list").append("<li><a onclick='return false;' href='#'> <img alt=\"#{marker.name}\" src=\"#{marker.gravatar_url}\" />  #{marker.name}</a></li>")

如何確定用戶選擇了哪個項目? 我所看到的所有內容似乎都要求您事先知道ID。

jQuery('#some_id')

向您的每個鏈接消息中添加一個click事件- this將引用<a>標記。 您可以用來做您想做的事。

$("#user_list a").click(function() {
    // this holds the item that was clicked.
    alert($(this).html());
});

示例: http//jsfiddle.net/KXGtt/

$("$user_list li").click(function() {
    console.log(this.index);
}

暫無
暫無

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

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