take the input field value of a gridview with jquery

Asked

Viewed 117 times

0

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

The HTML below was generated by GridView and I don’t know how to get the ID or NAME of input once they are created dynamically

<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, as suggested in the comment below and met my needs, but the sum only calculates the whole part disregarding the fractional part:

<script type="text/javascript">
    $(document).ready(function () {
        var _valor = 0;
        $("table#gridModal input").on('change', function () {
            _valor += parseFloat($(this).val());
        });
    });
</script>
  • Do you need to pick up the value of each separate input? If not just get them all with one $('table#gridModal input').

  • yes separate, because as I change the value of the input I will make the sum with the others

2 answers

1

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

For this, follows 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 see it running around here Jsfiddle

1


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

According to the answer to another question, the method parseFloat only considers the part of the string where you find more(+), less(-), number, exponent, or dot. When she finds anything else in the string, including the comma, she disregards and does the parse only in what was obtained at the beginning.

Follows your changed code with the fix on parseFloat:

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

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

Browser other questions tagged

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