[英]Renaming param created by javascript
I have a button that let's me create additional input field with every button press, which on submitting form saves every input field into one (array) parameter.我有一个按钮,让我可以在每次按下按钮时创建额外的输入字段,在提交表单时将每个输入字段保存到一个(数组)参数中。
Button erb looks like this:按钮 erb 如下所示:
<%= button_tag 'Add a new field', id: 'add_correct_answer', type: 'button' %>
While JavaScript behind it looks like this.而它背后的 JavaScript 看起来是这样的。
$(document).ready(function() {
// When the add_button is pressed
$('#add_correct_answer').click(function() {
// Container (already created)
var container = $('.buttons-fields');
// Create the input wrapper (input tag + delete button), and append it to `container`
var input_wrapper = $('<div>', { class: 'input-wrapper' }).appendTo(container);
// Create an input tag, and append it to input_wrapper
var input = $('<input>', { name: 'correct_answers[]', type: 'text' }).appendTo(input_wrapper);
// Create the remove button, append it to input_wrapper, and add a click callback to
// remove the input wrapper if it's pressed.
var remove = $('<span>', { text: 'x' }).appendTo(input_wrapper).click(function() {
input_wrapper.remove();
});
});
This function creates param that can be called with params[:correct_answers]
and it equals to array with input fields content.此函数创建可以使用
params[:correct_answers]
调用的params[:correct_answers]
,它等于具有输入字段内容的数组。
What I want to do is change that param to params[:task][:correct_answers]
.我想要做的是将该参数更改为
params[:task][:correct_answers]
。 How do I do that?我怎么做?
I've tried to change:我试图改变:
var input = $('<input>', { name: 'task[correct_answers[]]', type: 'text' }).appendTo(input_wrapper);
And it looks fine as parameter, but nothing is being saved into this.它作为参数看起来不错,但没有保存任何内容。
You can create a 2d array instead of a 1d array.您可以创建二维数组而不是一维数组。
1d array is like int[] a = [23,45,494,599]
Then you call with a[0]
or whatever as I am sure you know. int[] a = [23,45,494,599]
数组就像int[] a = [23,45,494,599]
然后你用a[0]
或我确定你知道的任何东西调用。
However the 2d array is a bit harder.然而,二维数组有点难。 The setup looks something like so:
设置看起来像这样:
int[] a =
[
[1,4,155,164,23,43],//This one
[8,75,45,3134,45,6],
[143,55,7,8,8754,5],
]
and called with a[0][3]
and would return 164. The first bracket choses the array inside the big array that I marked "This one" then the second number bracket thing gets the number inside that array.并使用
a[0][3]
调用并返回 164。第一个括号选择我标记为“This one”的大数组内的数组,然后第二个数字括号获取该数组内的数字。
Now that the explanation of the 2d array is done the real answer:现在对二维数组的解释已经完成,真正的答案是:
So 2 ideas.所以2个想法。 Try using the setup of
task[][]
and doing something like for(blah)task[I]=correct_answers;
尝试使用
task[][]
的设置并执行类似for(blah)task[I]=correct_answers;
You may not need the for but idk what you really need.您可能不需要 for 但知道您真正需要什么。 Or you can try doing
task[0][0]=correct_answers
but that one I have no idea if it would work.或者您可以尝试执行
task[0][0]=correct_answers
但我不知道它是否可行。 I think I could however so I kept it.我想我可以,所以我保留了它。
As OP said in the comments: " The thing is: I need to change params name. Parameters are not typical variables, so things like you mentioned above would be considered really bad practice. What could actually work, would be assigning params[:correct_answers] to params[:task][:correct_answers] and then deleting params[:correct_answers] before submitting form."正如 OP 在评论中所说:“事情是:我需要更改参数名称。参数不是典型的变量,所以像你上面提到的那样的事情将被认为是非常糟糕的做法。实际可行的方法是分配 params[:correct_answers ] 到 params[:task][:correct_answers],然后在提交表单之前删除 params[:correct_answers]。”
So my way won't work I guess.所以我猜我的方法行不通。 Unless you don't care about bad practices >:)
除非你不关心坏习惯>:)
All I had to do was change我所要做的就是改变
var input = $('<input>', { name: 'correct_answers[]', type: 'text' }).appendTo(input_wrapper);
into:进入:
var input = $('<input>', { name: 'task[correct_answers][]', type: 'text' }).appendTo(input_wrapper);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.