Skip to content Skip to sidebar Skip to footer

Percentage Of Two Number With Enter Key Works

I have a table that consists of and 's and I show the percentage of the sold tickets in the third . My code works correctly until I have an enter aft

Solution 1:

The problem is with your regex.

let v = $('td:nth-child(1)', $this).text().trim().split(/\r?\n/);

This line only returns the values, but concatenated.

What you want is a very basic regex (\d+) this will match only digits(All you need is to separate integers and text in a string).

Use this:

 let v = $('td:nth-child(1)', $this).text().trim().split(/(\d+)/).filter(v => v);

$('table tbody tr').each(function() {
  var $this = this,
    td2Value = $('td:nth-child(2)', $this).text().trim().split(/\D+/);
  $('span.result', $this).each(function (index, element) {

    let v = $('td:nth-child(1)', $this).text().trim().split(/(\d+)/).filter(v => v);
    if(v[index] != null && v[index].trim() == "Mytext")
    {
       v[index] = td2Value[index];
    }
    if(v[index] != null )
    {
      $(element).html(Math.round((td2Value[index] * 100 / v[index]) || 0) + '%');
    }

  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<table border="1">
             <thead>
                <tr>
                  <th> avalable</th>
                  <th> sold</th>
                  <th>  result </th>
                </tr>
            </thead>
<tbody>
<tr>
<td>   
10<br/>Mytext<br/></td>
<td> 5<br/>2<br/></td>
<td>
<span class="result"></span><br/>
<span class="result"></span><br/>
</td>        
</tr>
</tbody>
</table>

Post a Comment for "Percentage Of Two Number With Enter Key Works"