Skip to main content
conteúdo editado; título editado
Fonte Link
Leandro
  • 855
  • 6
  • 23

obter Setar o valor do input cujo o name tem colchetes

Conforme descrito no titulo, segue o exemplo abaixo:

---- 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 obtersetar nesse caso?
   //$("input[name=[22].value]").val("ZZZZZ")
   
    rawData = $('#teste input:hidden').serializeArray();
    var formData = JSON.stringify(rawData);
    
     alert(formData);
    
});

Para facilitar, coloquei no JSFiddle https://jsfiddle.net/leandrotss/jf5dg98o/4/

obter o valor do input cujo o name tem colchetes

Conforme descrito no titulo, segue o exemplo abaixo:

---- 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 obter nesse caso?
   //$("input[name=[22].value]").val("ZZZZZ")
   
    rawData = $('#teste input:hidden').serializeArray();
    var formData = JSON.stringify(rawData);
    
     alert(formData);
    
});

Para facilitar, coloquei no JSFiddle https://jsfiddle.net/leandrotss/jf5dg98o/4/

Setar o valor do input cujo o name tem colchetes

Conforme descrito no titulo, segue o exemplo abaixo:

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

Para facilitar, coloquei no JSFiddle https://jsfiddle.net/leandrotss/jf5dg98o/4/

Fonte Link
Leandro
  • 855
  • 6
  • 23

obter o valor do input cujo o name tem colchetes

Conforme descrito no titulo, segue o exemplo abaixo:

---- 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 obter nesse caso?
   //$("input[name=[22].value]").val("ZZZZZ")
   
    rawData = $('#teste input:hidden').serializeArray();
    var formData = JSON.stringify(rawData);
    
     alert(formData);
    
});

Para facilitar, coloquei no JSFiddle https://jsfiddle.net/leandrotss/jf5dg98o/4/