如何动态更改引导模式

时间:2015-10-08 10:09:07

标签: jquery bootstrap-modal

我有一些javascript代码的安静,允许用户点击会议并更改弹出的模式中的值。第一次有人点击所有工作正常,但如果我尝试更改第二次会议,模态继续显示第一个模态的数据。有谁知道我做错了什么? (包含HTML的数据存储在添加到页面中的div #modalwindow的内容中)...

$(document).delegate('button[wijzigattr="send"]', 'click', function() {
    // Check welke button is geclicked
    var id = $(this).attr("value")  ;
    alert(id)
    var content;
    var n = 0; 
    var id ={'ID' : id};
    $.post('http://www.openheidvanzaken.com/cardio/controller/getonderwijs2.php', id).done(function (data){
    $.each(data, function(key, value){
        $.each(value, function(key2, value2) {
            n++;
            switch (n){
                case 1:
                content = "<div class=\"modal fade\" id=\"wijzigonderwijs\" role=\"dialog\"><div class=\"modal-dialog\"><div class=\"modal-content\"><div class=\"modal-header\"><button type=\"button\" class=\"close\" data-dismiss=\"modal\">&times;</button><h4 class=\"modal-title\">Wijzig onderwijsmoment</h4></div><div class=\"modal-body\"><form action=\"controller/regonderwijs.php\" method=\"post\" role=\"form\" class=\"form-horizontal\"><div class=\"form-group\">"
                    break;
                case 2:
                content = content + "<label for=\"onderwijsnaam\" class=\"col-sm-2 control-label\">Naam</label><div class=\"col-sm-10\"><input type=\"text\" value=\"" + value2 + "\" id=\"onderwijsnaam\" name=\"Naam\"></div></div>"
                break;
                case 3:
                content = content + "<div class=\"form-group\"><label for=\"onderwijstitel\" class=\"col-sm-2 control-label\">Titel</label><div class=\"col-sm-10\"><input type=\"text\" value=\"" + value2 + "\" id=\"onderwijstitel\" name=\"Titel\"></div></div>"
                break;
                case 4:
                content = content + "<div class=\"form-group\"><label for=\"onderwijscategorie\" class=\"col-sm-2 control-label\">Categorie</label><div class=\"col-sm-10\"><input type=\"text\" value=\"" + value2 + "\" id=\"onderwijscategorie\" name=\"Categorie\"></div></div>"
                break;
                case 5:
                content = content + "<div class=\"form-group\"><label for=\"onderwijsdate\" class=\"col-sm-2 control-label\">Datum</label><div class=\"col-sm-10\"><input type=\"text\" value=\"" + value2 + "\" name=\"Datum\" id=\"onderwijsdatum\"></div></div></form></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-primary\" data-dismiss=\"modal\" id=\"wijzigonderwijs\">Wijzig</button></div></div></div></div>"
                alert (content);
                n = 0;
                break;
            }

    });
    });

    // Laad modal om wijziging in te voeren
    $('#modalwindow').empty();
    $('#modalwindow').replaceWith(content);
    $('#wijzigonderwijs').modal('show');
    });
})
})();

1 个答案:

答案 0 :(得分:2)

由于循环,有多个具有相同id的模态。当您通过id打开模态时,它将始终查找具有此id的第一个模态。解决方法是为每个模态分配唯一的ID。