亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何制作一個 HTML 數字輸入,將其值轉換為十分之一(可能使用 jQuery)

如何制作一個 HTML 數字輸入,將其值轉換為十分之一(可能使用 jQuery)

慕娘9325324 2023-10-24 20:40:31
假設我有一個輸入字段,例如<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' />


查看完整回答
反對 回復 2023-10-24
  • 1 回答
  • 0 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號