get the value of input fields from a gridview with jquery

0

I need to change the inputs fields of a GridView , and as they are changed I must display the sum of these inputs fields in another input that is in the footer of the screen and the question is: how to get the value of these input fields through JQuery ?

The HTML below was generated by GridView and I do not know how to get ID or NAME from input since they are dynamically created / p>
<table id="gridModal">
<tbody>
    <tr>
        <td>Abril/2016</td>
        <td>6</td>
        <td>11,61</td>
        <td>
        <input id="gridModal_txtQtdNovo_0" type="text" value="11,61" name="gridModal$ctl02$txtQtdNovo">
        </td>
        <td>6</td>
    </tr>
    <tr>
        <td>Maio/2016</td>
        <td>23</td>
        <td>44,52</td>
        <td>
        <input id="gridModal_txtQtdNovo_1" type="text" value="44,52" name="gridModal$ctl03$txtQtdNovo">
        </td>
        <td>21</td>
    </tr>
    <tr>
        <td>Junho/2016</td>
        <td>22</td>
        <td>42,58</td>
        <td>
        <input id="gridModal_txtQtdNovo_2" type="text" value="42,58" name="gridModal$ctl04$txtQtdNovo">
        </td>
        <td>20</td>
    </tr>
    <tr>
        <td>Julho/2016</td>
        <td>21</td>
        <td>40,65</td>
        <td>
        <input id="gridModal_txtQtdNovo_3" type="text" value="40,65" name="gridModal$ctl05$txtQtdNovo">
        </td>
        <td>19</td>
    </tr>
    <tr>
        <td>Agosto/2016</td>
        <td>23</td>
        <td>44,52</td>
        <td>
        <input id="gridModal_txtQtdNovo_4" type="text" value="44,52" name="gridModal$ctl06$txtQtdNovo">
        </td>
        <td>21</td>
    </tr>
</tbody>
</table>

This was the suggestion of DanielDutra , according to the suggestion in the comment below and met my needs, however the summation only calculates the whole part neglecting the fractioned part:

<script type="text/javascript">
    $(document).ready(function () {
        var _valor = 0;
        $("table#gridModal input").on('change', function () {
            _valor += parseFloat($(this).val());
        });
    });
</script>
    
asked by anonymous 21.04.2016 / 20:08

2 answers

1

As the other answer says, the problem really is the comma in the numbers.

According to the answer of another question , the parseFloat method only considers the part of the string where it finds most (+), minus (-), number, exponent, or period. When it finds anything else in the string, including the comma, it disregards and parses parse only at the beginning.

Follow your changed code with the correction in parseFloat :

<script type="text/javascript">
    $(document).ready(function () {
        var _valor = 0;
        $("table#gridModal input").on('change', function () {
            _valor += parseFloat($(this).val().replace('.', '').replace(',', '.'));
        });
    });
</script>

Once the sum is done, you may need to use replace('.', ',') to display the final value with the same formatting as inputs .

    
23.04.2016 / 08:02
1

Friend you need to treat the values that come from the inputs.

To do this, follow the solution ..

function numberFormat( num ) {
   var nFormat      =  num.replace(/\./g, '');
   nFormat  =  nFormat.replace(/\,/g, '.');
   return nFormat
}

var numero = "1.110,00";

alert( numberFormat( numero ) );

You can check it by running here JSFiddle

    
22.04.2016 / 05:20