固定基數 1000.00,點擊「上」加 1,點擊「下」減 1,類似數字微調器。
<style>
.spinner { display: inline-flex; border: 1px solid #ccc; border-radius: 4px; overflow: hidden; font-family: sans-serif; }
.spinner input { border: none; width: 60px; text-align: center; padding: 4px; outline: none; -moz-appearance: textfield; }
.spinner input::-webkit-outer-spin-button, .spinner input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.spinner button { background: #f0f0f0; border: none; width: 28px; cursor: pointer; font-size: 14px; line-height: 1; }
.spinner button:hover { background: #e0e0e0; }
.spinner button:active { background: #d0d0d0; }
</style>
<div class="spinner">
<button type="button" onclick="changeValue(this, -1)">−</button>
<input type="text" value="1000.00" readonly id="numInput">
<button type="button" onclick="changeValue(this, 1)">+</button>
</div>
<script>
function changeValue(btn, delta) {
var input = btn.parentElement.querySelector('input');
var val = parseFloat(input.value) || 0;
val = Math.max(0, val + delta); // 不小於 0
input.value = val.toFixed(2); // 保留兩位小數
}
</script>
<input type="number" value="1000" min="0" step="1" style="width:80px;">
瀏覽器自帶上下箭頭,支援鍵盤輸入、驗證,無需任何 JS/CSS。若需固定兩位小數,用 `step="0.01"` 並配合 JS 格式化。