2016-05-30 113 views
-2

我想用javascript刪除所有選中的元素,但它不起作用,有什麼建議嗎? 這是我的代碼Javascript功能不起作用?

<script> 
function deleteR(){ 

$('input[name=actionck]:checked').each(function() { 
    var id = $(this).attr("id"); 
$.ajax({ 
     type: "GET", 
     url: 'demo10_helper.jsp', 
     data: "command=delete&recordID=$id", 
     dataType: "xml", 
     cache: false, 
     success: function(xml) { 
      window.location.reload(true); 
      $("#recordgrid").trigger("reloadGrid"); 
     }, 
     error: function() { 
      alert("Failed to connect to API."); 
     } 
    }); 


}); 
} 

</script>  

這是我如何稱呼它:

<button type="button" onClick="deleteR()"> 
+1

什麼是你看到在控制檯中的錯誤。我認爲js函數正在工作,但您正在尋找觸發check的事件。在函數deleteR(){並檢查是否正在調用 – brk

+0

change [dataType:「json」],然後嘗試使用console.log或alert它 –

+0

@LinusKleen +1,但我仍然得到一個內部服務器錯誤 –

回答

1

在這一行代碼的:

$.ajax({ 
    type: "GET", 
    url: 'demo10_helper.jsp', 
    data: "command=delete&recordID=$id", // <--- this line 
    dataType: "xml", 
    // ... 
}); 

它看起來好像你想局部變量id插值到字符串。 Javascript不知道插值;你想要的是串聯

$.ajax({ 
    type: "GET", 
    url: 'demo10_helper.jsp', 
    data: "command=delete&recordID=" + id, // <--- concatenation 
    dataType: "xml", 
    // ... 
}); 
+0

很有效,謝謝! –

0

在行:

data: "command=delete&recordID=$id", 

您發送 「$ ID」 的字符串文字的一部分。也許你的意思是:

data: "command=delete&recordID=" + id, 

另外:

var id = $(this).attr("id"); 

會更有效,因爲:

var id = this.id; 

,少了很多類型。 :-)