2017-10-15 14 views
2

私はtrとtdsで構成されるテーブルを持っています.3番目のtd.Myコードに売れたチケットのパーセンテージを表示します。数字の代わりに "Mytext"また、書き込み率を表示するのではなく、無限大を表示します。どのようにしてtdが数字の代わりに "mytext"を持っているか定義し、そのテキストの代わりに最も近い数字を売り列に設定して計算するのですか?例えば、空きカラムにMytext、計算する?2つの数字のパーセンテージと別のtdの最終結果を表示

$('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+/); 
 
    $(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> 
 
Mytext<br> 
 
10<br/> 
 
</td> 
 
<td> 
 
5<br/> 
 
2<br/> 
 
</td> 
 
<td> 
 
<span class="result"></span><br/> 
 
<span class="result"></span><br/> 
 
</td> 
 
</tr> 
 
</tbody> 
 
</table>

答えて

3

それ

$('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) { 
 
    //change starts here 
 
    let v = $('td:nth-child(1)', $this).text().trim().split(/\r?\n/); 
 
    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) + '%'); 
 
    } 
 
    //change ends here 
 
    }); 
 
})
<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> 
 
Mytext<br> 
 
10<br/> 
 
</td> 
 
<td> 
 
5<br/> 
 
2<br/> 
 
</td> 
 
<td> 
 
<span class="result"></span><br/> 
 
<span class="result"></span><br/> 
 
</td> 
 
</tr> 
 
</tbody> 
 
</table>

+0

Mytextの変化に等しい場合は、値をチェックし、その後、available列内の正規表現の番号を削除します。ここで は私のコードですどうもありがとうございます。それは私のために働いた。しかし、私は質問がある、いくつかのケースでは、私は各tdの値を持っています。この場合、コードにエラーがあり、次の行の値を計算しません。 – inaz

+0

はい、たとえばこのtrの前に、別のtrがあり、trにはmytextのみが使用可能な列として、5が列として売られています。数。この場合、コードにエラーがあります – inaz

+0

はい、あなたのスニペットを編集しました – inaz

関連する問題