2016-10-09 8 views
0

Ich habe schon eine Weile nach diesem gesucht, aber ich konnte nicht herausfinden, wie es funktioniert. Ich bin neu in jQuery/AJAX und Bootstrap und ich hoffe, es ist nicht leicht ....Bootstrap Modal Replace Platzhalter

Ich habe diesen Knopf in einer Schleife

<button type="button" class="btn btn-default" data-toggle="modal" data-target="#info" data-added="<?php if(strtotime($ecp_code->added) > strtotime('2000-01-01 01:00:00')) { echo date('d.m.y H:i', strtotime($ecp_code->added)); } else { echo "-";} ?>" data-modified="<?php if(strtotime($ecp_code->changed) > strtotime('2000-01-01 01:00:00')) { echo date('d.m.y H:i', strtotime($ecp_code->changed)); } else { echo "-";} ?>"><i class="fa fa-info" style="width:14px;"></i></button> 

Das ist die Modal

<div class="modal fade" id="info" tabindex="-1" role="dialog" aria-labelledby="infoLabel" aria-hidden="true"> 
    <div class="modal-dialog modal-sm"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       Informations 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 
      </div> 
      <div class="modal-body"> 
       Added: PLACEHOLDER 
       Modified: PLACEHOLDER2 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>   
      </div> 
     </div> 
    </div> 
</div> 

Das Skript:

<script> 
    $(document).ready(function() { 
     $('#info').on('show.bs.modal', function (event) { 
      var button = $(event.relatedTarget); 
      var added = button.data('added'); 
      var modified = button.data('modified'); 

      var content = 'Added: ' + added + ' Modified: ' + modified + ''; 

      var modal = $(this); 
      modal.find('.modal-body').text(content); 
     }); 
    }); 
</script> 

Das funktioniert gut. Es ersetzt den gesamten Inhalt vom Modal-Body.


Aber jetzt möchte ich, dass nur ein einziges Wort ersetzt wird. PLACEHOLDER mit der var hinzugefügt und PLACEHOLDER2 mit der var geändert. (Es ist nur ein Beispiel)

<script> 
    $(document).ready(function() { 
     $('#info').on('show.bs.modal', function (event) { 
      var button = $(event.relatedTarget); 
      var added = button.data('added'); 
      var modified = button.data('modified'); 
      var modal = $(this); 
      var res = replace("PLACEHOLDER", (added)); 
      modal.find('.modal-body').text(res); 
     }); 
    }); 
</script> 

So etwas funktioniert nicht.

Noch eine Frage: Kennst du gute Ressourcen, wo ich jQuery und AJAX lernen kann?

Antwort

0

Legen Sie zwei neue Elemente in den Modal-Körper

<div class="modal-body"> 
    <label>Added:</label> 
    <div id="Added"></div> 
    <label>Modified:</label>  
    <div id="Modified"></div> 
</div> 

modal.find('.modal-body #Added').text(your content added); 

Und:

modal.find('.modal-body #Modified').text(your content modified); 

Wenn der Inhalt, den Sie auf diese divs hinzufügen html Tags enthält können Sie besser nutzen .html() anstelle von .text()

+0

Es funktioniert - danke! Eine wirklich gute Idee. Aber nur aus Interesse: Kann man ein einzelnes Wort in einem Modal ersetzen? – wassereimer

+0

Sie können ersetzen wie folgt content = content.replace (/ the_word/g, "other_word"); Dabei ist das_Wort das Wort, das ersetzt werden soll, und das_Wort das Ersetzen. Bitte markieren Sie diese Antwort als beantwortet. Es ist wichtig für zukünftige Benutzer. Bedenken Sie auch, dass die Ersetzungsmethode alle übereinstimmenden Wörter ersetzt. – Franco

+0

Bereits als Antwort markiert. :) Wenn ich die Ersetzung verwende, bekomme ich den Fehler "Uncaught ReferenceError: replace is not defined". content = content.replace (/ PLACEHOLDER1/g, hinzugefügt); modal.find ('. Modal-body'). Text (Inhalt); – wassereimer