英文:
jQuery convert checkbox selections to array
问题
我有一个类似以下的HTML:
<div class="pgggo-list-taxon">
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-56[]">
<div class="icon-box">
<div name="development">Development</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-14[]">
<div class="icon-box">
<div name="food">Food</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-8[]">
<div class="icon-box">
<div name="medical">Medical</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-1[]">
<div class="icon-box">
<div name="uncategorized">Uncategorized</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-2[]">
<div class="icon-box">
<div name="wordpress">WordPress</div>
</div>
</label>
</li>
</div>
每个复选框都有唯一的名称 = pgggo-category-sep-56
,pgggo-category-sep-14
。当多个项目被点击时,这些id被添加到下面数组的id字段。
我正在尝试有一个输出数组的方法:
$output = array(
array(
'taxonomy'=> 'category', // pgggo-category-sep-56 --category word is pulled from here
'id'=> array(56,14), // ids are pulled pgggo-category-sep-56 number at the end
)
array(
'taxonomy'=> 'home', // pgggo-home-sep-56 --homeword is pulled from here
'id'=> array(56,14), // ids are pulled pgggo-category-sep-56 number at the end
)
);
我尝试过这个:
$('.pgggo-container-ajax-sorting').on('click', '.pgggo-list-taxon input', function(event) {
var outputPgggo = $(this).attr('name');
}
但这只获取了字符串。有没有办法完成这个任务?
英文:
I have an HTML like this:
<div class="pgggo-list-taxon">
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-56[]">
<div class="icon-box">
<div name="development">Development</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-14[]">
<div class="icon-box">
<div name="food">Food</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-8[]">
<div class="icon-box">
<div name="medical">Medical</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-1[]">
<div class="icon-box">
<div name="uncategorized">Uncategorized</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-2[]">
<div class="icon-box">
<div name="wordpress">WordPress</div>
</div>
</label>
</li>
</div>
Each checkbox has unique name = pgggo-category-sep-56
, pgggo-category-sep-14
. As multiple items are clicked the ids are added to the id field of below array.
I am trying to have a method which gives me an array as output:
$output = array(
array(
'taxonomy'=> 'category', // pgggo-category-sep-56 --category word is pulled from here
'id'=> array(56,14), // ids are pulled pgggo-category-sep-56 number at the end
)
array(
'taxonomy'=> 'home', // pgggo-home-sep-56 --homeword is pulled from here
'id'=> array(56,14), // ids are pulled pgggo-category-sep-56 number at the end
)
);
I tried this:
$('.pgggo-container-ajax-sorting').on('click', '.pgggo-list-taxon input', function(event) {
var outputPgggo = $(this).attr('name');
}
But this only pulls the string. Is there any way to get this done?
答案1
得分: 1
我建议将这些 ID 存储在一个对象中,而不是一个数组的数组中。这样更容易处理。可以像这样构建对象:
const array = {
category: [],
home: [],
};
然后,你可以使用一些数组操作随时将其转换为所需的形式:
Object.entries(array).map(([taxonomy, id]) => ({ taxonomy, id }))
以下是在复选框点击时更新对象的代码:
const array = {};
$('[name^="pgggo-"]').on('click', function() {
const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
const id = attr.split('[')[0];
const checked = $(this).prop('checked');
array[taxonomy] = array[taxonomy] || [];
const index = array[taxonomy].indexOf(id);
index == -1 ? array[taxonomy].push(id) : array[taxonomy].splice(index, 1);
console.log(array);
});
或者,你可以只在需要使用时构建数组,而不必处理重复和状态:
$('.sort-button').on('click', function() {
const array = {};
$('[name^="pgggo-"]').filter(':checked').each(function() {
const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
array[taxonomy] = array[taxonomy] || [];
array[taxonomy].push(attr.split('[')[0]);
});
console.log(array);
});
希望这可以帮助你解决问题。
英文:
I would recommend storing the ids in an object instead of having an array of array. It's easier to work with. Something like this:
const array = {
category: [],
home: [],
};
Then, you can always transform into the desired form with some array manipulation:
Object.entries(array).map(([taxomony, id]) => ({taxomony, id}))
Here is the code to update the object when a checkbox is clicked:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array = {};
$('[name^="pgggo-"]').on('click', function() {
const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
const id = attr.split('[')[0];
const checked = $(this).prop('checked');
array[taxonomy] = array[taxonomy] || [];
const index = array[taxonomy].indexOf(id);
index == -1 ? array[taxonomy].push(id) : array[taxonomy].splice(index, 1);
console.log(array);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="pgggo-list-taxon">
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-56[]">
<div class="icon-box">
<div name="development">Development</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-14[]">
<div class="icon-box">
<div name="food">Food (category)</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-home-sep-14[]">
<div class="icon-box">
<div name="food">Food (home)</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-8[]">
<div class="icon-box">
<div name="medical">Medical</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-1[]">
<div class="icon-box">
<div name="uncategorized">Uncategorized</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-2[]">
<div class="icon-box">
<div name="wordpress">WordPress</div>
</div>
</label>
</li>
</div>
<!-- end snippet -->
Or, instead of having to go through the trouble of handling duplicates and states. You can construct the array only when you need to use it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('.sort-button').on('click', function() {
const array = {};
$('[name^="pgggo-"]').filter(':checked').each(function() {
const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
array[taxonomy] = array[taxonomy] || [];
array[taxonomy].push(attr.split('[')[0]);
});
console.log(array);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="pgggo-list-taxon">
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-56[]">
<div class="icon-box">
<div name="development">Development</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-14[]">
<div class="icon-box">
<div name="food">Food (category)</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-home-sep-14[]">
<div class="icon-box">
<div name="food">Food (home)</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-8[]">
<div class="icon-box">
<div name="medical">Medical</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-1[]">
<div class="icon-box">
<div name="uncategorized">Uncategorized</div>
</div>
</label>
</li>
<li>
<label>
<input type="checkbox" name="pgggo-category-sep-2[]">
<div class="icon-box">
<div name="wordpress">WordPress</div>
</div>
</label>
</li>
</div>
<button class="sort-button">Show!</button> <!-- for demo purposes -->
<!-- end snippet -->
答案2
得分: 1
你想要将选定的值存储/发送到一个数组中,对吗?那么为什么你对每个复选框使用唯一的名称呢?!...如果它们属于一组,那么请适当地命名它们,如下所示:
<input type="checkbox" name="pgggo-category-sep[]" value="56">
<input type="checkbox" name="pgggo-category-sep[]" value="14">
<input type="checkbox" name="pgggo-category-sep[]" value="8">
<input type="checkbox" name="pgggo-category-sep[]" value="1">
<input type="checkbox" name="pgggo-category-sep[]" value="2">
<input type="checkbox" name="pgggo-home-sep[]" value="foo">
<input type="checkbox" name="pgggo-home-sep[]" value="bar">
//....
然后它们将在提交时自动作为数组发送,无需进一步重新排列...或者如果你真的想以ajax方式做,你可以轻松地获取所有类别复选框的id,如下所示:
$('[name="pgggo-category-sep[]"]').on('change', function () {
let values = Array.from($('[name="pgggo-category-sep[]"]:checked'))
.map(elem => $(elem).val())
})
英文:
You want to store/send the selected values in one array, right? So why you use unique names then for each checkbox?! ... if they belong together then name them appropriately like so:
<input type="checkbox" name="pgggo-category-sep[]" value="56">
<input type="checkbox" name="pgggo-category-sep[]" value="14">
<input type="checkbox" name="pgggo-category-sep[]" value="8">
<input type="checkbox" name="pgggo-category-sep[]" value="1">
<input type="checkbox" name="pgggo-category-sep[]" value="2">
<input type="checkbox" name="pgggo-home-sep[]" value="foo">
<input type="checkbox" name="pgggo-home-sep[]" value="bar">
//....
Then they will be sent as an array on submit automatically, no further rearrangement needed ... or if you really want to do it the ajax way still, you can easily get all the ids for the category-checkboxes like so:
$('[name="pgggo-category-sep[]"]').on('change', function () {
let values = Array.from($('[name="pgggo-category-sep[]"]:checked'))
.map(elem => $(elem).val())
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论