简体   繁体   中英

Listview duplicates ID on adding list item Jquery Mobile

I am building a custom Jquery mobile list where I have a domain list(li) which has few flip switch.

I am dynamically creating the flip switches. When creating, unique ID's were assigned. I checked when creating and all have the unique ID's. After creating an flip switch, am appending to the list item.

After refreshing list, I can see the flip-boxes having the same ID's like other Flip-boxes. I verified when creating and it was fine. After creating, it malformed.

Am using Foundation JS to construct the grid.

The JavaScript code which does this is,

for(var currentIndex = 0; currentIndex < myArray.length ; currentIndex ++){

    var currentField =  myArray[currentIndex];

    $('#my-list').append('<li data-role="list-divider" class="my-list-by-domain">'+currentField.Name+'</li>');

    for(var domainCount = 0; domainCount < (currentField.UserGoals).length ; domainCount++) {

        var currentMyGoalField = (currentField.UserGoals)[domainCount];

        var myGoalsDataElement = $(".my-goals-content-template .my-goals-data-row").clone();

        var switchElement =  ('<fieldset><div data-role="fieldcontain">' +

            '<select class="my-goals-flip-switch" data-role="flipswitch" onchange="goalOnChange(' + currentMyGoalField.UserGoalId + ')" ' +

            'id="my-goals-toggle-' + currentMyGoalField.UserGoalId + '">' +

            '<option value="true">Yes</option>' +

            '<option value="false">No</option>' +

            '</select></div>' +

            '</fieldset>');

        $(".my-goals-label", myGoalsDataElement).text(currentMyGoalField.Name);

        $("#my-goals-complete-check").empty();

        $("#my-goals-complete-check").append(switchElement);

        $('#my-goal-list').append(myGoalsDataElement).enhanceWithin();

    }

}

The HTML container for the same is ,

<div class="my-goals-content-template" style="display: none">

    <div class="row my-goals-data-row">

        <div class="small-9 medium-10 large-10 columns my-goals-label"></div>

        <div class=" small-3 medium-2 large-2 columns">

            <div id="my-goals-complete-check">

            </div>

        </div>

    </div>

</div>

Can anyone help me in understanding this.

I found the issue.

Actually I have to specify the container where the control resides.

The fix is,

$("#my-goals-complete-check",myGoalsDataElement).empty();

$("#my-goals-complete-check",myGoalsDataElement).append(switchElement); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM