Set the value of the input whose name has brackets

1

As described in the title, follow the example below:

---- HTML

<form id="nameGenderForm">
<table id="teste">
     <tr>
            <th >Name</th>
            <th >Gender</th>
        </tr>
     <tr>
            <td id="11"><input type="hidden" name="11" value=""></td>
            <td  id="12"><input type="hidden" name="12" value=""></td>
        </tr>
    <tr>
            <td  id="21"><input type="hidden" name="[21].value"  value=""></td>
            <td  id="22"><input type="hidden" name="[22].value"  value=""></td>
        </tr>
</table>
    <input type="submit" />
</form>

---- SCRIPT

$("#nameGenderForm").submit(function(event) {
    event.preventDefault();

   $("input[name=11]").val("AAAA")
    $("input[name=22]").val("BBBB")


   //$("input[name=[21].value]").val("FFFF")  // aqui: como setar nesse caso?
   //$("input[name=[22].value]").val("ZZZZZ")

    rawData = $('#teste input:hidden').serializeArray();
    var formData = JSON.stringify(rawData);

     alert(formData);

});

To facilitate, put it in JSFiddle link

    
asked by anonymous 08.11.2015 / 18:16

2 answers

2

You can do this:

$('input[name="[21].value"]').val("AAAA");

Fiddle with example:

  

link

    
08.11.2015 / 18:29
1

There is a css selector for this:

$('[name*="["]').val("novo valor");

It will look for those who contain anywhere in the name bracket a [ bracket.

w3schools example.

    
08.11.2015 / 19:55