javascript
  • jquery
  • 2016-09-22 1 views 0 likes 
    0

    Wie Click-Ereignis tot Taste in Zeichenfolge

    $.ajax({ 
     
    \t \t url:'/getArticles', 
     
    \t \t method:'GET', 
     
    \t }).done(function(articles){ 
     
    \t \t var content = ''; 
     
    \t \t articles.forEach(function(e){ 
     
    \t \t \t var res = "<div class='article'>" + 
     
    \t \t \t \t \t \t "<h3>" + e.title + "</h3>" + 
     
    \t \t \t \t \t \t "<p>" + e.content + "</p><br>" + 
     
    \t \t \t \t \t \t "<button onclick=crud.remove(" + e._id + ")>Remove</button><br>" + 
     
    \t \t \t \t \t "</div>"; 
     
    \t \t \t content += res; 
     
    \t \t }); 
     
    \t \t $('#allarticles').append(content); 
     
    \t }); 
     
    \t window.crud = (function(){ 
     
    \t \t // Remove an article 
     
    \t \t function remove(id){ 
     
    \t \t \t console.log(id); 
     
    \t \t }

    Wie kann ich einfügen e._id richtig hier hinzufügen, so dass es id des Artikels gestellt wird?

    Wenn ich auf diese Schaltfläche klicken, um es sagt:

    (index):1 Uncaught SyntaxError: Invalid or unexpected token

    +0

    den Parameter in Anführungszeichen: '"
    "' –

    Antwort

    0

    Es Syntaxfehler ist jQuery in Ihrer Schaltfläche Erstellung verwenden. Sie haben einzelne Anführungszeichen für die ID verpasst. Auch Sie haben einige Zahnspangen vermisst.

    <button onclick=crud.remove(" + e._id + ")>Remove</button><br> 
    

    Ersetzen Sie Ihre obige Zeile mit diesem

    <button onclick=crud.remove('" + e._id + "')>Remove</button><br> 
    

    habe ich Ihren Code korrigiert:

    $.ajax({ 
     
    \t \t url : '/getArticles', 
     
    \t \t method : 'GET', 
     
    \t }).done(
     
    \t \t \t function(articles) { 
     
    \t \t \t \t var content = ''; 
     
    \t \t \t \t articles.forEach(function(e) { 
     
    \t \t \t \t \t var res = "<div class='article'>" + "<h3>" + e.title 
     
    \t \t \t \t \t \t \t + "</h3>" + "<p>" + e.content + "</p><br>" 
     
    \t \t \t \t \t \t \t + "<button onclick=crud.remove('" + e._id 
     
    \t \t \t \t \t \t \t + "')>Remove</button><br>" + "</div>"; 
     
    \t \t \t \t \t content += res; 
     
    \t \t \t \t }); 
     
    \t \t \t \t $('#allarticles').append(content); 
     
    \t \t \t }); 
     
    \t window.crud = (function() { 
     
    \t \t // Remove an article 
     
    \t \t function remove(id) { 
     
    \t \t \t console.log(id); 
     
    \t \t } 
     
    \t });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    Verwandte Themen