简体   繁体   English

如何将多个选择选项字段的值发送到控制器

[英]How can I send values of multiple select option fields to a controller

I have a form for a model "Cell" that can be cloned using javascript .我有一个可以使用javascript克隆的模型“Cell”的表单。 I have both text fields and select fields in the form that can be cloned.我有可以克隆的表单中的文本字段和选择字段。 Since the cloned element produces an array of Strings, I have sent the text elements as an array by changing the name attribute of the field using the format: cell[ ]['name'] .由于克隆的元素生成一个字符串数组,我通过使用以下格式更改字段的 name 属性将文本元素作为数组发送: cell[ ]['name'] However, f.select field in rails comes with a default name attribute cell[category_id] .但是, f.select字段带有默认名称属性cell[category_id] Due to this, I keep getting the error:因此,我不断收到错误消息:

Invalid request parameters: expected Hash (got Array) for param `cell'.

This is the code:这是代码:

= form_for @cell do |f|
  #clone
    .form.rowField
      = f.text_field :name, name: 'cell[][name]', class: "form-control"
      = f.select :category_id, options_for_select(Category.all.pluck('name'))
      i.fas.fa-plus-circle#addrow
    = f.submit "Save", class: "button btn btn-primary"

The javascript code for cloning the form is克隆表单的 javascript 代码是

$('#addrow').click(function () {
        var row = document.querySelector('.rowField');
        var cloneRow = row.cloneNode(true);
        var target = document.querySelector('#clone');
        cloneRow.querySelectorAll('input').forEach(function (input) {
            input.value = "";
        });
        target.appendChild(cloneRow);
    });

Is there a way to change to name attribute of the select tag from cell[category_id] to cell[][category_id] ?有没有办法将 select 标签的 name 属性从cell[category_id]更改为cell[][category_id]

我通过将选择字段的名称更改为:

= f.select :category_id, options_for_select(Category.all.pluck('name')), {}, {name: 'cell[][category_id]'}

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

相关问题 SQL 发送的每个选项有 3 个值的多选 - multiple select with 3 values for each option to SQL send 如何在 javascript 变量中存储在选择选项中传递的多个值? - How can I store multiple values passed in the select option in the javascript variable? 如何有效地将相同的选项值分配给Meteor中的多个Select元素? - How can I efficiently assign the same option values to multiple Select elements in Meteor? 如何通过select的选项值发送xml变量? - How can I send xml variable by select's option value? 如何将多个选择字段中的值存储到数组并在选项更改时更新数组? - How to store values from multiple select fields to an array and update array on option change? Reactjs,我该如何添加选择选项属性值来声明 - Reactjs, How can I add select option attributes values to state 当我单击同一选择的另一个选项时,如何才能获得多个选择的未选择选项? - how I can get unselected an option in a select multiple when I click in another option of that same select? 如何遍历HTML选择 <option>点击价值? - How can I cycle through HTML select <option> values on click? 如何将值数组从输入发送到控制器 - How can I send an array of values from the inputs to the controller 如何在 Javascript 中的选择中创建多个选项 - How I can create multiple option in select in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM