1
我有一個表格,其中包含<tr>
和<td>
,我在第三個<td>
中顯示出售門票的百分比。我的代碼能夠正常工作,直到我在每個<td>
中的<br/>
標記之後都有一個輸入爲止。例如,如果您在每個<br/>
標籤後按Enter鍵,我的代碼將正常工作,否則它不起作用,並且不會計算兩個數字的百分比。我如何定義我的代碼,不考慮每個<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(/\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) + '%');
}
});
})
<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>
謝謝。我編輯了我的正則表達式,但是它計算了wronge的百分比。例如,第一行返回520% – inaz