在特定索引中填充JS数组

时间:2022-07-18 21:16:44

Hi so this may sound odd but lets say currently I am doing this:

嗨所以这可能听起来很奇怪,但我们现在说这样做:

 var post_data  = $('input[name="checks[]"]:checked').map(function() {
         return this.value;
        }).get();

to get contents of multiple checkboxes into an array to send via AJAX. Now what I ACTUALLY want to do is to construct post_data in a specific way.

将多个复选框的内容放入要通过AJAX发送的数组中。现在我真正想做的是以特定的方式构造post_data。

I want the value of the checkbox to be stored in the location of the array matching its value. I hope that makes sense :S

我希望复选框的值存储在与其值匹配的数组的位置。我希望这是有道理的:S

so lets say my values of the checkboxes were:

所以我要说我的复选框值是:

1,5,3,7

1,5,3,7

i want post_data to be constructed like:

我希望post_data构造如下:

post_data[1] = 1
post_data[5] = 5 etc.

post_data [1] = 1 post_data [5] = 5等

ignoring the fact this may seem stupid, can anyone help me out, I am struggling right now. This is what I was trying, but my very slim knowledge is getting the better of me:

无视这个看似愚蠢的事实,任何人都可以帮助我,我现在正在努力。这就是我的尝试,但我非常渺茫的知识让我变得更好:

EDIT UPDATE: (code progress)

编辑更新:(代码进度)

var post_data = $('input[name="checks[]"]:checked').serialize();

        alert(post_data);

        $.post('search_item.php', { 'checks[]':post_data,..

many thanks,

非常感谢,

3 个解决方案

#1


0  

you should initialize post_data as an object

你应该将post_data初始化为一个对象

var post_data = {};

here's a simple example

这是一个简单的例子

#2


1  

You are close. value is a string. You want a number. Use parseInt() to turn that string into a number:

你很亲密value是一个字符串。你想要一个号码。使用parseInt()将该字符串转换为数字:

post_data[parseInt(value)] = value;

#3


0  

I'd like to propose a solution using HTML and serialize(). If you construct each check box with the array number in the name then you will get the result that you are looking for.

我想提出一个使用HTML和serialize()的解决方案。如果您使用名称中的数组编号构造每个复选框,那么您将获得您要查找的结果。

<input type="checkbox" name="checkbox[3]" value="3" class="retain_array_value_checkbox" />
<input type="checkbox" name="checkbox[5]" value="5" class="retain_array_value_checkbox" />
<input type="checkbox" name="checkbox[1]" value="1" class="retain_array_value_checkbox" />

Now the JS:

现在JS:

var post_data  = $('input.retain_array_value_checkbox:checked').serialize();

I can't test this, but it should work. Simple and clean :)

我不能测试这个,但它应该工作。简单干净:)

#1


0  

you should initialize post_data as an object

你应该将post_data初始化为一个对象

var post_data = {};

here's a simple example

这是一个简单的例子

#2


1  

You are close. value is a string. You want a number. Use parseInt() to turn that string into a number:

你很亲密value是一个字符串。你想要一个号码。使用parseInt()将该字符串转换为数字:

post_data[parseInt(value)] = value;

#3


0  

I'd like to propose a solution using HTML and serialize(). If you construct each check box with the array number in the name then you will get the result that you are looking for.

我想提出一个使用HTML和serialize()的解决方案。如果您使用名称中的数组编号构造每个复选框,那么您将获得您要查找的结果。

<input type="checkbox" name="checkbox[3]" value="3" class="retain_array_value_checkbox" />
<input type="checkbox" name="checkbox[5]" value="5" class="retain_array_value_checkbox" />
<input type="checkbox" name="checkbox[1]" value="1" class="retain_array_value_checkbox" />

Now the JS:

现在JS:

var post_data  = $('input.retain_array_value_checkbox:checked').serialize();

I can't test this, but it should work. Simple and clean :)

我不能测试这个,但它应该工作。简单干净:)