【发布时间】:2021-12-23 23:09:45
【问题描述】:
我对 jquery 有点陌生,很抱歉问这个问题。
我正在使用数据表,并使用以下代码创建了一个引导下拉菜单。
{ '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>' } },
我想要的是在用户单击class="searchtarget" href="#">Search</a> 链接并将其发布到 php 文件时获取复选框。我正在尝试获取选中的属性并在以下脚本中测试帖子。但是即使选中,帖子也是0。
<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>
这是全部检查后的发布结果。都应该是1。当用户只选择几个时,只有那些需要为1的。
USA: 0
NL: 0
Italy: 0
China: 0
GB: 0
我做错了什么?
编辑:尝试了 Kinglish 的答案。但是得到一个错误。 Uncaught ReferenceError: selected is not defined我不明白,它说未定义选定的。当我使用data: {selected: data}, }); 时,它是但随后我在我的帖子中得到以下结果。是这样吗?
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>
编辑 2
当用户使用搜索链接时,尝试从输入框中获取键入的值以及选定的框。但它不会出现.. 有什么问题,谁能解释一下?
$(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; });
【问题讨论】:
-
有谁能帮忙吗? :(
标签: jquery datatables