我在表格的第三列中有进度条。我从我评论使用的td替换progressbar的值,并从我评论的td替换进度条的最大值。
我的代码问题与请求文本有关。每当我写这个文本而不是数字我的代码不起作用甚至显示错误。如果你写一个数字而不是"一个请求"我的代码工作正常。我如何使用" onrequest"为零?我的意思是我想将非请求文本假设为0。
这是我的代码:
$("table tr").each(function() {
var children = $(this).children('td'); //get all td children of current tr
var vals = $(children[1]).html().split('<br>'); //get an array of values by splitting on <br>
var maxVals = $(children[0]).html().split('<br>'); //likewise get an array of max values by splitting on <br>
var progressBars = $(this).find('progress'); // find all progress elements inside current tr.
vals.forEach((val, index) => { //iterate over vals array
$(progressBars[index]).val(parseInt(val)); //use index to set val for correct progressBar
$(progressBars[index]).prop('max', parseInt(maxVals[index]));
})
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tr>
<td>on request <br/> 30</td> <!--available-->
<td>4 <br/> 5</td> <!--used-->
<td colspan="2">
<progress value="98" max="100"><div class="graph"></div></progress><br/>
<progress value="98" max="100"><div class="graph"></div></progress>
</td>
</tr>
<tr>
<td>20 <br/> 20</td> <!--available-->
<td>6 <br/> 5</td> <!--used-->
<td>
<progress value="50" max="100"><div class="graph"></div></progress><br/>
<progress value="50" max="100"><div class="graph"></div></progress>
</td>
</tr>
</table>
&#13;
答案 0 :(得分:2)
简单if语句可以解决问题:
$("table tr").each(function() {
var children = $(this).children('td'); //get all td children of current tr
var vals = $(children[1]).html().split('<br>'); //get an array of values by splitting on <br>
var maxVals = $(children[0]).html().split('<br>'); //likewise get an array of max values by splitting on <br>
var progressBars = $(this).find('progress'); // find all progress elements inside current tr.
vals.forEach((val, index) => { //iterate over vals array
var value = (val.trim() === "on request") ? 0 : parseInt(val);
var max = (maxVals[index].trim() === "on request") ? 0 : parseInt(maxVals[index]);
$(progressBars[index]).val(val); //use index to set val for correct progressBar
$(progressBars[index]).prop('max', max);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tr>
<td>on request <br/> 30</td> <!--available-->
<td>4 <br/> 5</td> <!--used-->
<td colspan="2">
<progress value="98" max="100"><div class="graph"></div></progress><br/>
<progress value="98" max="100"><div class="graph"></div></progress>
</td>
</tr>
<tr>
<td>20 <br/> 20</td> <!--available-->
<td>6 <br/> 5</td> <!--used-->
<td>
<progress value="50" max="100"><div class="graph"></div></progress><br/>
<progress value="50" max="100"><div class="graph"></div></progress>
</td>
</tr>
</table>