Multiple inputs with same name

Aleksey Solomakha

I have form, in it user can duplicate input forms, and I have a trouble with a radio buttons. When I trying to use:

$('form').serialize();

In Chrome Dev Tools

I've got:

"text-field%5B%5D=%D0%98%D0%B2%D0%B0%D0%BD%D0%BE%D0%B2+%D0%98%D0%B2%D0%B0%D0%BD+%D0%98%D0%B2%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87&text-h-alignment%5B%5D=42&text-v-alignment%5B%5D=42&text-field%5B%5D=(000)+000+00+00&text-alignment%5B%5D=center& text-h-alignment%5B%5D=42&text-v-alignment%5B%5D=42"

As you can see, radio buttons are not in "array", it's an unique variable. But I use the "[]" symbols at the end of variable name.

Here is my HTML:

<form>
  <div class="row">
    <div class="col-lg-4">
      <div class="input-group">
        <div class="input-group-btn">
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Шаблон <span class="caret"></span>
          </button>
          <ul class="dropdown-menu">
            <li><a href="#" onclick="$(this).closest('.input-group').find('input[type=text]').val('Иванов Иван Иванович');">Имя</a></li>
            <li><a href="#" onclick="$(this).closest('.input-group').find('input[type=text]').val('(000) 000 00 00');">Телефон</a></li>
            <li><a href="#" onclick="$(this).closest('.input-group').find('input[type=text]').val('[email protected]');">E-mail</a></li>
            <li><a href="#" onclick="$(this).closest('.input-group').find('input[type=text]').val('г. Город, ул. Улица 1');">Адрес</a></li>
            <li><a href="#" onclick="$(this).closest('.input-group').find('input[type=text]').val('example.com');">Сайт</a></li>
          </ul>
        </div>
        <input type="text" class="form-control" name="text-field[]">
      </div>
    </div>
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default active">
        <input type="radio" checked="checked" name="text-alignment[]" value="left"><span class="glyphicon glyphicon-align-left" aria-hidden="true"></span>
      </label>
      <label class="btn btn-default">
        <input type="radio" name="text-alignment[]" value="center"><span class="glyphicon glyphicon-align-center" aria-hidden="true"></span>
      </label>
      <label class="btn btn-default">
        <input type="radio" name="text-alignment[]" value="right"><span class="glyphicon glyphicon-align-right" aria-hidden="true"></span>
      </label>
    </div>

    <div class="input-group spinner padding-15 pull-left">
      <span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span></span>
      <input type="text" class="form-control" value="42" name="text-h-alignment[]">
      <div class="input-group-btn-vertical">
        <button class="btn btn-default" type="button"><i class="glyphicon glyphicon-chevron-up"></i></button>
        <button class="btn btn-default" type="button"><i class="glyphicon glyphicon-chevron-down"></i></button>
      </div>
    </div>

    <div class="input-group spinner padding-15 pull-left">
      <span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></span>
      <input type="text" class="form-control" value="42" name="text-v-alignment[]">
      <div class="input-group-btn-vertical">
        <button class="btn btn-default" type="button"><i class="glyphicon glyphicon-chevron-up"></i>
        </button>
        <button class="btn btn-default" type="button"><i class="glyphicon glyphicon-chevron-down"></i>
        </button>
      </div>
    </div>

  </div>
  <div class="row margin-top-15">
    <div class="col-lg-4">
      <a href="#" class="color-green" onclick="$(this).closest('.container-fluid').find('.row:eq(0)').clone().prependTo($(this).closest('.container-fluid form'))">
        <div class="circle green-circle circle-position pull-left"><span class="glyphicon glyphicon glyphicon-plus color-white circle-plus" aria-hidden="true"></span>        </div>Добавить еще поля</a>
    </div>
  </div>
</form>
Aleksey Solomakha

I did not find a native solution in HTML.

Therefore it was necessary to use such a solution to the JS:

$(this).closest('.container-fluid').find('.row:eq(0)').clone(true).find('input[type=radio]').attr('name', function(i, val){return val.replace(/(\d+)/, function(n){ return ++n })});

As a result, when you click on "duplicate field", appears radio buttons with the names: text-alignment[1], text-alignment[2]...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Submitting multiple inputs with same name

From Dev

Add multiple inputs with same name to mysql database

From Dev

Two inputs with the same name

From Dev

how to get value of Multiple text inputs with same name

From Dev

Yii2 validation rule for multiple inputs with same name

From Dev

Request data from form with multiple inputs using the same name

From Dev

javascript synchronize inputs with same name

From Dev

Multiple form inputs for the same field

From Dev

validating multiple inputs at the same time

From Dev

How does MVC decide which value to bind when there are multiple inputs with the same name?

From Dev

How to put multiple Bootstrap inputs on same line?

From Dev

Same error check for multiple inputs with jQuery

From Dev

Django - Submitting form with multiple inputs for same field

From Dev

Multiple parameters by the same name

From Dev

Multiple functions with the same name

From Dev

Multiple divs with same name

From Dev

Codeigniter: How do I pass multiple inputs with multiple same names?

From Dev

Codeigniter: How do I pass multiple inputs with multiple same names?

From Dev

Capybara how i can fill_in all inputs with the same name?

From Dev

target inputs with same name different forms on 1 page with php

From Dev

Capybara how i can fill_in all inputs with the same name?

From Dev

Count value of inputs with same name and copy final value to another input

From Dev

how do i save three inputs to a file with same name as the user?

From Dev

in codeigniter, i am using same name for multiple inputs(type="text"), during submit i want to allow at least one value, how can i validate?

From Dev

Multiple EJB beans with same name

From Dev

Multiple Inheritance: same variable name

From Dev

Multiple external packages with same name

From Dev

Multiple actions with same action name

From Dev

Laravel - multiple controllers with same name

Related Related

  1. 1

    Submitting multiple inputs with same name

  2. 2

    Add multiple inputs with same name to mysql database

  3. 3

    Two inputs with the same name

  4. 4

    how to get value of Multiple text inputs with same name

  5. 5

    Yii2 validation rule for multiple inputs with same name

  6. 6

    Request data from form with multiple inputs using the same name

  7. 7

    javascript synchronize inputs with same name

  8. 8

    Multiple form inputs for the same field

  9. 9

    validating multiple inputs at the same time

  10. 10

    How does MVC decide which value to bind when there are multiple inputs with the same name?

  11. 11

    How to put multiple Bootstrap inputs on same line?

  12. 12

    Same error check for multiple inputs with jQuery

  13. 13

    Django - Submitting form with multiple inputs for same field

  14. 14

    Multiple parameters by the same name

  15. 15

    Multiple functions with the same name

  16. 16

    Multiple divs with same name

  17. 17

    Codeigniter: How do I pass multiple inputs with multiple same names?

  18. 18

    Codeigniter: How do I pass multiple inputs with multiple same names?

  19. 19

    Capybara how i can fill_in all inputs with the same name?

  20. 20

    target inputs with same name different forms on 1 page with php

  21. 21

    Capybara how i can fill_in all inputs with the same name?

  22. 22

    Count value of inputs with same name and copy final value to another input

  23. 23

    how do i save three inputs to a file with same name as the user?

  24. 24

    in codeigniter, i am using same name for multiple inputs(type="text"), during submit i want to allow at least one value, how can i validate?

  25. 25

    Multiple EJB beans with same name

  26. 26

    Multiple Inheritance: same variable name

  27. 27

    Multiple external packages with same name

  28. 28

    Multiple actions with same action name

  29. 29

    Laravel - multiple controllers with same name

HotTag

Archive