Set the value of the input whose name has brackets

Asked

Viewed 261 times

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);

});

For ease, I put in Jsfiddle https://jsfiddle.net/leandrotss/jf5dg98o/4/

  • What is your intention to use the brackets ?

  • I don’t know pq but partial view mounted table ssim. Maybe pq instead of using ienumerable or use ilist.

  • Soen: http://stackoverflow.com/questions/4608487/how-do-i-reference-an-element-by-name-with-brackets-in-it

  • ienumerable ? ilist? that would be something with C# ?

  • Yes. I just need to figure out how to set/fetch that element

2 answers

2


1

There is a css selector for that reason:

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

Go find someone who contains anywhere in the attribute name a clasp [.

w3schools example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.