2016-01-26 52 views
2

我有問題插件:Codemirror。 插件工作良好,但當我嘗試連接引導模式,這是問題。 3)當我關閉第一個模態並且第二次運行時:3代碼塊(重複) 3)當我關閉第二個模態並運行時第三時間是:6個碼塊(一式兩份)CodeMirror - 乘法代碼塊

我的代碼:

<div class="modal fade" id="code-editor" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
     <h4 class="modal-title" id="">Edytor elementu</h4> 
     </div> 
     <div class="modal-body"> 
     <div class="row"> 
      <div class="col-lg-12"> 
      <div id="code"></div> 
      </div> 
     </div> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal" name="close">Anuluj</button> 
     <button type="button" class="btn btn-primary" data-dismiss="modal" name="save">Zapisz</button> 
     </div> 
    </div> 
    </div> 
</div> 
<script type="text/javascript"> 
$(function(){ 
    $('[data-element-id]').append('<i class="glyphicon glyphicon-pencil edit-element"></i>').css({position: 'relative'}); 
    $('body').on('click', '[data-element-id] i.edit-element', function(){ 
    var code = $(this).parent(); 
    var code_new = code.clone(); 
    code_new.find('i.edit-element').remove(); 
    $('#code-editor').modal('show'); 

    $('#code-editor').on('shown.bs.modal', function() { 
     editor = CodeMirror($('#code-editor #code')[0], { 
     mode: "text/html", 
     lineNumbers: false, 
     value: '<h1>Example text</h1>' 
     }); 
    }); 
    $('#code-editor button[name="save"]').on('click',function(){ 
     code.html(editor.getValue()).append('<i class="glyphicon glyphicon-pencil edit-element"></i>'); 
    }); 
    }); 
}); 
</script> 
<link rel="stylesheet" href="assets/front/plugins/codemirror/lib/codemirror.css"> 
<script src="assets/front/plugins/codemirror/lib/codemirror.js"></script> 
<script src="assets/front/plugins/codemirror/mode/xml/xml.js"></script> 

和圖象: result image

回答

1

我有一個解決方案:

使用.off('click') 例如:

$('body').off('click').on('click', '[data-element-id] i.edit-element', function(){