2016-03-02 61 views
0

我想用Kendo UI建立一个数字输入部件。我能够设置格式,以便在字段没有焦点时尾随零不被截断,但是当字段确实有焦点时,尾随的零消失。如何在Kendo UI的numericTextBox中保留尾随零?

我有一个例如在http://dojo.telerik.com/eBevU和相关位列举如下:

<div id="add-product" class="demo-section k-content"> 
    <ul id="fieldlist"> 
    <li> 
     <label for="numeric">Input</label> 
     <input id="numeric" type="number" value="17" min="0" max="100" step="1" style="width: 100%;" /> 
    </li> 
    </ul> 
</div> 

<script> 
    $(document).ready(function() { 
    // create NumericTextBox from input HTML element 
    $("#numeric").kendoNumericTextBox({ 
     format: "n5", 
     decimals: 5 
    }); 
    }); 
</script> 

此示例示出了在输入框“17.00000”之前使其具有焦点,和“17”之后。

回答

0

唯一的方法(我所经历的)kendo在聚焦时将保留小数位数,如果该数字已经具有指定精度的值,否则将其删除。您可以尝试和修改该步骤,以便用户可以通过较小的金额增加该值。

$("#numeric").kendoNumericTextBox({ 
    format: "n5", 
    decimals: 5, 
    step: 0.00001 
}); 

祝你好运。

0

我发现的唯一解决方案是基本上使用小部件配置中指定的格式重新构建值,即添加小数点分隔符(如果需要)和缺少的尾随零。将下面的函数绑定到输入的焦点事件。

function() { 
    var input = $(this); 
    var widget = input.data("kendoNumericTextBox"); 

    setTimeout(function() { 
     if (widget !== undefined && input.val() !== "") { 
      var format = widget._format(widget.options.format); 
      var decimals = widget.options.decimals; 

      if (decimals > 0) { 
       var inputValue = input.val(); 
       var digits = inputValue.split(format["."]); 

       if (digits.length == 1) 
        inputValue = inputValue + format["."]; 

       var l = digits[0].length + 1 + decimals; 
       while (inputValue.length < l) 
        inputValue = inputValue + "0"; 

       input.val(inputValue); 
      } 
     } 
    } 
} 

你可以找到here你的例子修改。