kikou kikoku kikou kikoku - 6 months ago 12
HTML Question

Javascript function doesn't work?

I am trying to delete all checked elements with javascript,but it doesn't work,any suggestions?
This is my code

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


this is how i call it :

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

Answer

In this line of your code:

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

it looks as though you'd like to interpolate the local variable id into the string. Javascript does not know interpolation; what you want is concatenation:

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