Calculate percentage in dynamic field

-1

I have inputs type text where a function in javascript takes these values from the input and makes a percentage account, works correctly, the problem is that depending on the data input these fields become dynamic, generating several inputs with the same ID, then the javascript function can not reference and does not calculate. Does anyone have any idea how I could solve this or change the method for the calculation?

PS: The system calculated before the data received in php, but the user needs to see the result of the account in real time when he enters the percentage value.

    
asked by anonymous 09.06.2016 / 18:21

1 answer

0

"there will be 3 inputs with the same ID" I do not know how your code is, but just to show you that you can retrieve everything from the other side of the php - even with elements of the same id.

I suggest you dynamically generate the id (I do not know if that's your question) but here's a code that shows you how to pass information and retrieve with POST in php.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="test.php" method="post">
        <input id="as"  type="text" name="valor[]" value="1"><br>
        <input id="as"  type="text" name="valor[]" value="2"><br>
        <input id="as"  type="text" name="valor[]" value="3"><br>
        <input id="as"  type="submit" value="Submit">

        </form>

        <?php

        $arr=$_POST["valor"];
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
        ?>
    </body>
</html>

Notice what happens in the POST array:

TestthiscodetoretrievevaluesusingclassesandnotIds

//HTML<formaction="test.php" method="post">
  <input id="as" class="tosend" type="text" name="valor[]" value="1"><br>
  <input id="as" class="tosend" type="text" name="valor[]" value="2"><br>
  <input id="as" class="tosend" type="text" name="valor[]" value="3"><br>
  <input id="btntosend" class="btntosend" type="submit" value="Submit">
</form>
//Js
$('#btntosend').on( 'click', function(e) { 
    e.preventDefault();
   $('.tosend').each(function() {//Passar por cada radio da classe rdShipMo
      alert($(this).val());
   });   
});

See the fiddle . You do not even need the ID.

    
09.06.2016 / 22:06