2017-10-12 1 views
-1

버튼이있는 HTML 표가 있습니다. 버튼을 클릭하면 내 백엔드 서비스에 대한 GET 호출을 수행하는 ajquery 함수를 호출합니다.잡히지 않은 ReferenceError : jquery에 'abc'가 정의되어 있지 않습니다.

Html 페이지 행은 다음과 같이 생성됩니다.

!DOCTYPE html> 
<html> 
<head> 
<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"> 
</script> 
<style> 
table, th, td { 
    border: 1px solid black; 
} 
</style> 
<meta charset="UTF-8"> 
<title>Clients</title> 
</head> 
<body> 
<table style="width:100%" id="clients_data"> 
<caption>Clients</caption> 
    <tr> 
    <th>Clients</th> 
    <th>Number of Sites</th> 
    <th>Reset the Processing</th> 
    </tr> 
    </table> 

<!-- --Java script Functions --> 

<script> 
function loadCustomers() { 
    $.ajax({ 
     type: 'GET', 
     url: 'http://localhost:8080/cache/getCustomers', 
     dataType: 'json', 
     success: function(data) { 
      var rows = []; 
      $.each(data,function(id,value) { 
       rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset('+id+')">Reset</td></tr>'); 
      }); 
      $('table').append(rows.join('')); 
     } 
    }); 
}; 


function reset(id) { 
    alert(id) 
    $.ajax({ 
     type: 'GET', 
     url: 'http://localhost:8080/cache/reset?clientName='+id, 
     success: function(data) { 
      alert("Reset success") 
     } 
    }); 
}; 

window.onload = loadCustomers; 
</script> 
</body> 
</html> 

내 리셋 기능은 다음과 같습니다. 내가 버튼을 클릭 할 때

function reset(id) { 
    alert(id) 
    $.ajax({ 
     type: 'GET', 
     url: 'http://localhost:8080/cache/reset?clientName='+id, 
     success: function(data) { 
      alert("Reset success") 
     } 
    }); 
}; 

, 나는 그것이

클릭 버튼 내가 도착하면 내 리셋() 함수에 'ID'값을 전달하는 기대;

ReferenceError: testClient is not defined 
    at HTMLButtonElement.onclick ((index):1) 
onclick @ (index):1 

무엇이 잘못 되었습니까?

+1

어디에 질문에 코드를''태그를 닫는? – guest271314

+2

testClient를 참조하는 코드 부분을 표시 할 수 있습니까? 그것이 오류를 치는 곳입니다. – Nick

+1

표시된 코드에서 'testClient'와 아무런 관련이 없습니다. – charlietfl

답변

2

당신은 행을 만들 때 :

// This is your code 
$.each(data,function(id,value) { 
    rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset('+id+')">Reset</td></tr>'); 
}); 

당신이 id 버튼의 onclick 속성을 추가 할 수 있습니다. 그것은 다음과 같이합니다 :

<button type="button" onclick="reset(testClient)"> 

당신은 그것을 같이 할 : 그 여분의 따옴표없이

<button type="button" onclick="reset('testClient')"> 

, 당신은의 값을 전달하고, 문자열을 통과하지 않습니다 변수 (정의되지 않음).

이 솔루션은 HTML 템플릿에 그 따옴표를 추가하는 것입니다

$.each(data,function(id,value) { 
    rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset(\''+id+'\')">Reset</td></tr>'); 
}); 
+0

감사합니다. 내 실수를 이해해야합니다. – Ratha

관련 문제