2

現在、SQLデータベースのチーム名が入力されたドロップダウンリストがあります。

$(".show_hide2").click(function () {
    ("#team").find("tr:gt(0)").remove();
    $(".slidingDiv2").slideToggle();
    teams = $.parseJSON(getTeams());
    for (i = 0; i < teams.length; i++) {
        var team = getTeam(teams[i]);
        updateTeamBoard(team);
        populateTeamSelection(team);
    }
}

ドロップダウンにデータを入力するためのコードは次のとおりです。

JS:

function populateTeamSelection(team) {
    var team = $.parseJSON(team);
    $("#teamSelection").find("#tr:gt(0)").remove();
    $("<option value='" + team.teamID + "'>" + team.teamName + "</option>").appendTo("#teamSelection");
}

HTML:

<div class="slidingDiv2">
    <select id="teamSelection">
        <option id="default" value="0">Select A Team</option>
    </select>
    <input type="button" id="teamViewer" value="Team Viewer"></input>
</div>

問題は、[表示/非表示]ボタンをクリックするたびに、リスト内の現在の情報が保持され、同じ情報がリストに追加されることです。私はAJAXを使用してテーブルとリストを動的に生成していますが、何らかの理由でこれを理解できず、かなり単純だと感じています。どんな助けでも大歓迎です。

4

2 に答える 2

2

クリックするたびに、populateTeamSelection()が呼び出されます。呼び出されるたびpopulateTeamSelection()に、リストに何かが追加されます。手っ取り早い解決策は、リストからすべてのアイテムを削除してから、それらを追加することです。

ループの前にすべてのアイテム$('select').children().remove()を削除するために使用できます。optionfor

于 2012-06-21T21:43:23.373 に答える
2
$(function ($)
{
    $populateTeamSelection = function(team)
    {
        $result = $.parseJSON(team,function(call){$handle = call;});
        $result.success(function(){ $data = $handle; });
        $result.error(function()
        {
            //Do something or nothing on error
        });
        $result.complete(function()
        {
            //Clear all current options first
            $('#teamSelection').html('');

            //Populate with new data
            $.each($data,function(lable,value)
            {
                $("#teamSelection").append($("<option></option>").attr("value",value['teamID']).text(value['teamName']));
            });
        });
    };
}(jQuery));

$(document).ready(function()
{
    $('.show_hide2').click(function()
    {
        $(".slidingDiv2").slideToggle();
        $populateTeamSelection(team);
    });
});
于 2012-06-21T21:53:21.647 に答える