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

    어떻게 </p> <p>

    $.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 }
    가 어떻게 제대로 여기 e._id를 삽입 할 문자열에 버튼 어린 아이 클릭 이벤트를 추가하려면?

    내가 말하는이 버튼을 클릭하면 : jQuery를 사용하여 버튼 만들기에 구문 오류가 있습니다

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

    +0

    는 따옴표로 매개 변수를 넣어 :' "<버튼의 onclick을 = \ "crud.remove ('"+ e._id +' ") \"> 제거
    "' –

    답변

    0

    . ID에 작은 따옴표가 누락되었습니다. 또한 괄호를 놓치고있었습니다.

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

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

    하여 위의 라인을 교체 난 당신의 코드를 수정했습니다 :

    $.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>

    관련 문제