I am a bit new to jquery so sorry for asking.
I am using datatables and I created a bootstrap dropdown with the folowing code.
{ 'data': null, title: '', wrap: true, "render": function (item) { return '<div class="dropdown"><button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><i class="glyphicon glyphicon-cog"></i><span class="caret"></span></button><ul class="dropdown-menu checkbox-menu allow-focus" aria-labelledby="dropdownMenu1"><li role="presentation" class="dropdown-header">Detectives</li><input type="text" class="form-control" id="exampleInputPassword1" placeholder="Accountname"><li ><label><input type="checkbox" class="usa"> United States</label></li><li ><label><input type="checkbox" class="netherlands"> Netherlands</label></li><li ><label><input type="checkbox" class="Italy"> Italy</label></li><li ><label><input type="checkbox" class="china"> China</label></li><li ><label><input type="checkbox" class="gb"> Great Britain</label></li><li role="presentation" class="divider"></li><li role="presentation"><a role="menuitem" tabindex="-1" class="searchtarget" href="#">Search</a></li></ul></div>' } },
What I want is to get the checked boxes when the user clicks the class="searchtarget" href="#">Search</a> link and post it to a php file. I am trying to get the checked property and testing the post in the folowing script. But the post is 0 even when checked.
<script>
$("#accountTable").on("change", "input[type='checkbox']",".checkbox-menu", function() {
$(this).closest("li").toggleClass("active", this.checked);
});
$("#accountTable").on('click', '.allow-focus', function (e) {
e.stopPropagation();
});
</script>
<script>
$(document).ready(function () {
$("#accountTable").on("click", ".searchtarget", function () { // notice the on function
var SearchUSA = $(this).closest("li","input[type='checkbox']", ".usa").is(':checked') ? 1 : 0;
var SearchNL = $(this).closest("li","input[type='checkbox']", ".netherlands").is(':checked') ? 1 : 0;
var SearchItaly = $(this).closest("li","input[type='checkbox']", ".italy").is(':checked') ? 1 : 0;
var SearchChina = $(this).closest("li","input[type='checkbox']", ".china").is(':checked') ? 1 : 0;
var SearchGB = $(this).closest("li","input[type='checkbox']", ".gb").is(':checked') ? 1 : 0;
$.ajax({
type: "POST",
url: "testchecked.php",
data: {
USA: SearchUSA,
NL: SearchNL,
Italy: SearchItaly,
China: SearchChina,
GB: SearchGB
},
});
return true;
});
});
</script>
This is the post result when all is checked. It should all be 1. our when a user only selects a few only those need to be 1.
USA: 0
NL: 0
Italy: 0
China: 0
GB: 0
What am I doing wrong?
EDIT: Tried the answer of Kinglish. But get a error. Uncaught ReferenceError: selected is not defined I dont get it, it says selected is not defined. When I use data: {selected: data}, }); it is but then I get the folowing result in my post. Is this right?
selected[selected][]: netherlands
selected[selected][]: Italy
<script>
$(document).ready(function () {
$("#accountTable").on("click", ".searchtarget", function () { // notice the on function
$(this).closest('ul').find('input[type=checkbox]:checked')
.map(function() { return this.value; }).get();
let data = {
selected: $(this).closest('ul').find('input[type=checkbox]:checked').map(function() {
return this.value;
}).get()
}
$.ajax({ type: "POST", url: "testchecked.php", data: {selected: selected}, }); return true; });
});
</script>
EDIT 2
Trying to get the typed value from the input box together with the selected boxes when the user uses the search link. But it wont show up.. What is wrong, can anyone explaing ?
$(this).closest('ul').find('input[type=text]').map(function() { return this.val; }).get();
let dataname = {
findtarget: $(this).closest('ul').find('input[type=text]').map(function() {
return this.val;
}).get()
}
var findtarget = $(this).closest('ul').find('input[type=text]').val();
$.ajax({ type: "POST", url: "testchecked.php", data: data,dataname,findtarget:findtarget }); return true; });