【问题标题】:Add positive sign in Kendo NumericTextBox在 Kendo NumericTextBox 中添加正号
【发布时间】:2014-06-17 20:08:15
【问题描述】:

我有一个 Kendo NumericTextBox。此文本框允许正数和负数。

正如预期的那样,负数有一个“-”前缀。

是否可以在正数上加上“+”前缀?

我正在使用 ASP.NET MVC 5。这是一个代码示例:

@Html.Kendo().NumericTextBoxFor(model => model.PositveNegative).Step(0.25f)

对此的任何帮助将不胜感激。

谢谢。

阿布拉尔

【问题讨论】:

    标签: asp.net-mvc kendo-ui kendonumerictextbox


    【解决方案1】:

    您可以使用 Change 和 Spin 事件处理程序。 这是javascript版本的代码。

    $("#inputID").kendoNumericTextBox({
                            format: "+#",
                            change: function() {
                              var value = this.value();                             
                              if(value>0) this.options.format="+#";
                              else this.options.format="#";
    
                            },
                           spin: function() {
                              var value = this.value();
                              if(value>0) this.options.format="+#";
                              else this.options.format="#";
                          }
                        });
    

    【讨论】:

    • 感谢您提供代码示例。它为正数添加了一个 +。但是,有一个问题,负数也有 + 前缀。
    • 也为“spin”添加处理程序
    • 抱歉,让我澄清一下。我将您的示例与自旋处理程序一起使用。我仍然遇到同样的问题,如果我输入 NumericTextBox 或使用向上向下箭头,负值显示为 +-0.25、+-0.5、+-0.75 等。我在 else 中放置了一个警报(值)子句,我得到了预期的结果-0.25。就好像格式没有被应用一样。你还有什么建议吗?感谢您的帮助。
    【解决方案2】:

    这里以 Cocococo 先生的回答为起点,为您提供 MVC 包装器版本:

     @(Html.Kendo().NumericTextBox().Name("Test").Step(0.25f)
    .Events(events => events.Change("Testing").Spin("Testing"))
      )
    
    
    
    
        <script>
    
            function Testing()
            {
    
                var numeric = $("#Test").val();
    
                if (numeric > 0)
                {
                    $("#Test").kendoNumericTextBox({ format: "+##.##", decimals: 2 });
                }
                else
                {
                    $("#Test").kendoNumericTextBox({ format: "##.##", decimals: 2 });
    
                }
                console.log(numeric);
            }
    
        </script>
    

    这适用于打字或使用微调器,应该会给你想要的结果。

    【讨论】:

    • 这对我有用。我非常感谢您和 @mr-cocococo 的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多