日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

javascript-2個(gè)狀態(tài)的帶有舍入的舍入數(shù)字文本框

 印度阿三17 2019-10-26

我想要一個(gè)帶有2種狀態(tài)的html數(shù)字文本框,當(dāng)聚焦時(shí),它必須顯示所有小數(shù)位,而當(dāng)聚焦丟失時(shí),僅顯示2個(gè)小數(shù).我?guī)缀鯇?shí)現(xiàn)了.

HTML

<input data-bind="attr: { 'data-numericvalue': valueToRound}" class="numerictextbox"
       type="number"/>

Javascript:

var viewModel = {
    valueToRound: ko.observable(7.4267),
};

//NUMERIC TEXTBOX BEHAVIOUR
$('.numerictextbox').focusout(function () {
  $(this).attr("data-numericvalue", this.value); //this line does not update the viewModel
  this.value = parseFloat($(this).attr("data-numericvalue")).toFixed(2);
});
$('.numerictextbox').focusin(function () {
  if ($(this).attr("data-numericvalue") !== undefined) this.value = $(this).attr("data-numericvalue");
});

ko.applyBindings(viewModel); 

Jsfiddle:https:///7zzt3Lbf/64/

但是我的問題是,發(fā)生聚焦時(shí),它不會(huì)更新綁定屬性(在這種情況下為viewModel).這是我的代碼的簡(jiǎn)化版本,因此我希望它對(duì)我的實(shí)際場(chǎng)景中的許多屬性通用.

解決方法:

您混入了太多的jQuery ??

淘汰賽具有事件綁定和hasFocus綁定以處理UI輸入.

在下面的示例中,我創(chuàng)建了一個(gè)視圖模型,該模型具有可觀察到的隱藏realValue,該值存儲(chǔ)未修改的輸入.當(dāng)showDigits為false時(shí),displayValue將此數(shù)字限制為2位數(shù)字.

我使用過hasFocus來跟蹤我們是否要顯示整數(shù):它鏈接到showDigits.

var ViewModel = function() {
  this.showDigits = ko.observable(true);
  
  var realValue = ko.observable(6.32324261);

  this.displayValue = ko.computed({
    read: function() {
      return this.showDigits() 
        ? realValue()
        : parseFloat(realValue()).toFixed(2);
    },
    write: realValue
  }, this);
};


ko.applyBindings(new ViewModel()); 
<script src="https://cdnjs./ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input data-bind="value: displayValue, hasFocus: showDigits" type="number"/>

編輯:在注釋中,計(jì)算式的代碼過多了:這是將計(jì)算式的邏輯包裝在可重用的擴(kuò)展器中的方法:

ko.extenders.digitInput = function(target, option) {
  var realValue = target,
      showRealValue = ko.observable(false),
      displayValue = ko.computed({
        read: function() {
          return showRealValue() 
            ? realValue()
            : parseFloat(realValue()).toFixed(2);
        },
        write: realValue
      }, this);
  
  displayValue.showRealValue = showRealValue;
  
  return displayValue;
};


var ViewModel = function() {
  this.value1 = ko.observable(6.452345).extend({ digitInput: true });
  this.value2 = ko.observable(4.145).extend({ digitInput: true });
};


ko.applyBindings(new ViewModel());
<script src="https://cdnjs./ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<input data-bind="value: value1, hasFocus: value1.showRealValue" type="number"/>
<input data-bind="value: value2, hasFocus: value2.showRealValue" type="number"/>
來源:https://www./content-1-522901.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多