我有一个带有CPT(custom post type的缩写)“bagp\\u deals”和自定义分类法“ba\\u locations”和“ba\\u cats”的站点,基本上它的post type“deals”以“Location”和“Categories”作为层次分类法。在默认的编辑屏幕上,我想将选择限制为每个选项中的一个(一个位置和一个类别),并且我正在尝试使用JQuery来实现这一点,我注意到ba\\U位置的字段自定义分类命名为“tax\\u input[ba\\U locations][]”,到目前为止,我有以下代码:
jQuery("input[name=tax_input[ba_locations][]]").click(function () {
selected = jQuery("input[name=tax_input[ba_locations][]]").filter(":checked").length;
if (selected > 1){
jQuery("input[name=tax_input[ba_locations][]]").each(function () {
jQuery(this).attr("checked", false);
});
jQuery(this).attr("checked", true);
}
});
假设witch将复选框选择限制为一个。由于某种原因,我不能让它工作。The Question
所以问题是为什么这不起作用?或者您是否有更好的解决方案将选择限制为一个?非常感谢您的帮助。
更新:
这是我使用的工作代码:jQuery("input[name=\\"tax_input[ba_locations][]\\"]").click(function () {
selected = jQuery("input[name=\\"tax_input[ba_locations][]\\"]").filter(":checked").length;
if (selected > 1){
jQuery("input[name=\\"tax_input[ba_locations][]\\"]").each(function () {
jQuery(this).attr("checked", false);
});
jQuery(this).attr("checked", true);
}
});