2017-10-12 6 views
-1

ボタン付きのhtmlテーブルがあります。ボタンをクリックすると、バックエンドサービスへのGET呼び出しを行うajquery関数が呼び出されます。キャッチされていないReferenceError; 'abc'がjqueryで定義されていません

次のように作成されたページ行。

!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>'); 
}); 

あなたはボタンのonclick属性にidを追加します。それは次のようになります:

<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

関連する問題