Answer the question
In order to leave comments, you need to log in
Why isn't an ajax request sent when a checkbox is clicked?
When reading a json file, I form the data output in html. Html code is generated normally. I use the received data from json to form checkboxes (livelist_more function). When clicking on checkboxes, the request should be sent to the send function, but for some reason it is not sent.
PS
The error in my opinion is that the function does not see the function. Tried different options, but without success.
Please help me figure out what is wrong and, if possible, an example.
//data sending function
function send(data){
$.ajax({
type: "get",
url: url_send,
data: data,
cache: false,
success: function(html){
$('#result2').append(data+'<br>');
}
});
return false;
}
$(document).ready(function(){
$('input[type="checkbox"]').each(function () {
$(this).change(function(){
if($(this).attr("checked")){
send($(this).attr("name")+'='+1);
}else{
send($(this).attr("name")+'='+0);
}
});
});
//функция для разбора json и формирования html кода
function livelist_more (){
$.getJSON(file.json, function(data) {
var items = [];
$.each(data, function(key,val) {
items.push('<li><label>' + val.name + '(' + val.id + ') <input type="checkbox" name="ch_' + val.id + '" id="ch_' + val.id + '" /></li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('#livelist_el');
});
};
livelist_more ();
} );
Answer the question
In order to leave comments, you need to log in
$('input[type="checkbox"]').each(function () {
Processes only currently existing checkboxes
$('#livelist_el').on('change', 'input[type="checkbox"]', function(){
if($(this).attr("checked")){
send($(this).attr("name") + '=' + 1);
} else {
send($(this).attr("name") + '=' + 0);
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question