简体   繁体   English

将选中的复选框放入数组

[英]Put checked checkboxes into an array

TO CLARIFY I'VE GOT THE VALUES OF THE CHECKED CHECKBOXES. 为了澄清,我已经检查了复选框的值。 I NEED TO ORGANIZE THEM IN AN ARRAY SIMILAR TO THE PHP EXAMPLE GIVEN. 我需要将它们组织成类似于给出的PHP示例的阵列。 THANKS 谢谢

I'm trying to build an array of checked checkboxes to be sent over AJAX for processing by PHP. 我正在尝试构建一系列选中的复选框,以通过AJAX发送以供PHP处理。 Originally I got the selected checkboxes by PHP but I'm having trouble converting this process to Javascript. 最初,我是通过PHP选中的复选框,但是在将此过程转换为Javascript时遇到问题。

There are three different groups of checkboxes ('first', 'second', 'third') and for each of these groups there are 4 different checkboxes that can be checked. 共有三个不同的复选框组(“第一”,“第二”,“第三”),并且对于每个组,可以选中4个不同的复选框。

The html of the checkboxes followings the same pattern up to value 4. 复选框的html遵循相同的模式,最高为4。

<input type="checkbox" name="first" class="selection first"  value="1"  />
<input type="checkbox" name="second" class="selection second" value="1"  />
<input type="checkbox" name="third" class="selection third" value="1"  />
<input type="checkbox" name="first" class="selection first"  value="2"  />
<input type="checkbox" name="second" class="selection second" value="2"  />
<input type="checkbox" name="third" class="selection third" value="2"  />

and so on.... 等等....

The PHP code would return an array of the checked checkboxes like such. PHP代码将返回诸如此类的选中复选框的数组。

$selections => array(
['first'] => array(
[0] => 1,
[1] => 3,
[2] => 4),
['second'] => array(
[0] => 2),
['third'] => array(
[0] => 1,
[1] => 4)
);

I've been trying and trying to replicate this in JavaScript but I keep getting errors such as cannot convert undefined to object and the likes. 我一直在尝试并尝试在JavaScript中复制它,但我不断遇到错误,例如cannot convert undefined to object等。 I loop the available positions to create arrays, then loop all the checkboxes and if checked, I get the value. 我循环可用位置以创建数组,然后循环所有复选框,如果选中,则得到该值。

console.log() prints the position ('first', 'second' or 'third') and the number of the selection (1 - 4). console.log()打印位置(“第一”,“第二”或“第三”)和选择的编号(1-4)。 That's all I need, but how do I put this in an array similar to the PHP one? 这就是我所需要的,但是如何将其放入类似于PHP的数组中?

function getSelections() {

var selections = new Array();

$('.position').each(function() {
    selections[$(this).attr('value')] = new Array;
});

$('.selection').each(function() {
    if( $(this).prop('checked') == true ) {
        position = $(this).attr('name');
        selection = $(this).attr('value');

        console.log(position + ' - ' + selection);
    }
});
return selections;

} }

It's to be sent to a PHP script as JSON and I need to decode it as an array like the PHP one. 它将以JSON的形式发送到PHP脚本,我需要将其解码为类似于PHP数组的数组。

Nothing fancy needed.... change to this and view the posted arrays. 不需要任何花哨的东西。...更改为该样式并查看发布的数组。

<input type="checkbox" name="first[]" class="selection first"  value="1"  />
<input type="checkbox" name="second[]" class="selection second" value="1"  />
<input type="checkbox" name="third[]" class="selection third" value="1"  />
<input type="checkbox" name="first[]" class="selection first"  value="2"  />
<input type="checkbox" name="second[]" class="selection second" value="2"  />
<input type="checkbox" name="third[]" class="selection third" value="2"  />

Then serialize this as normal to submit via ajax instead. 然后将其正常序列化以通过ajax提交。

jQuery :选中的选择器可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM