2016-05-30 178 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; 

,少了很多类型。 :-)