您可以使用 regx:
/^0+/
删除不必要的零。我将提供示例代码。
function bruto() {
var value = document.getElementById("serialResults").innerHTML;
// Remove zeros and kg
var result = value.replace(/^0+/, '').replace('kg', '');
document.getElementById("25").value = result;
}
function tara() {
var value = document.getElementById("serialResults").innerHTML;
// Remove zeros and kg
var result = value.replace(/^0+/, '').replace('kg', '');
document.getElementById("35").value = result;
}
<div id="serialResults">0000300kg</div>
<button class="tmbltimbang" id="timbangmuatan" onclick="bruto()">BRUTO</button>
<button class="tmbltimbang" id="timbangkosong" onclick="tara()">TARA</button>
BRUTO <input readonly type="text" name="25" id="25" value=""></input>
TARA <input readonly type="text" name="35" id="35" value=""></input>