jquery or JS create child element and assign an ID

This question already has an answer here:

  • Creating a div element in jQuery [duplicate] 26 answers

  • Try this

    $("#fDistList").append('<option id="'+ item.ID + '">' + item.GROUP_NAME + '</option>');
    

    When you do

    $("option").attr('id', item.ID);
    

    you are reselecting all option elements and setting their ID attribute.


    你可以像这样做,一次通过

    $('<option/>',{
            text: item.GROUP_NAME, 
            id:item.ID
        }).appendTo('#fDistList');
    
    链接地址: http://www.djcxy.com/p/83348.html

    上一篇: 使用jQuery $('<div />')选择一个元素

    下一篇: jquery或JS创建子元素并分配一个ID