假設我有一個輸入字段,例如<input type='number' id='input-tenths' />我想要實現的目標是將我放入該字段的數字視為“十分之一”而不是整數,例如:當我在此輸入字段中輸入 a 時3,我希望該字段的值為0.3之后,當我輸入 a 時5,我希望該值變為3.5...然后 a 9,該值應變為35.9到目前為止,我已經嘗試添加一個隱藏字段<input type='hidden' id='hidden-tenths'/>和一個類似的函數$('#input-tenths').on('input', function(e){ $('#hidden-tenths').val($('#hidden-tenths').val() + '' + $(this).val().slice(-1)); $(this).val($('#hidden-tenths').val() / 10); $('#hidden-tenths').val($(this).val() * 10);});但這并不總是按預期工作(例如,當我在原始輸入中退格時,會附加而不是刪除一個數字,正如使用.slice(-1).什么是實現預期行為的好方法,我試圖避免顯式綁定所有按鍵,從而實質上重新編程輸入字段(除非這是實現這一目標的唯一方法)。$('#input-tenths').on('input', function(e) { $('#hidden-tenths').val($('#hidden-tenths').val() + '' + $(this).val().slice(-1)); $(this).val($('#hidden-tenths').val() / 10); $('#hidden-tenths').val($(this).val() * 10);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type='number' id='input-tenths' /><input type='number' id='hidden-tenths' />
1 回答

動漫人物
TA貢獻1815條經驗 獲得超10個贊
高興時將不透明度從 0.3 更改為 0。您可能想要隱藏小數
$('#hidden-tenths').on('input', function(e) {
$('#input-tenths').val((this.value/10).toFixed(1))
});
#input-tenths { opacity:1; position:absolute; z-index:0; }
#hidden-tenths { opacity:.3; position:absolute; z-index:100; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='number' id='input-tenths' />
<input type='number' id='hidden-tenths' />
- 1 回答
- 0 關注
- 151 瀏覽
添加回答
舉報
0/150
提交
取消