简体   繁体   English

如何通过回滚Kendo UI中的上一个有效值来撤消输入值

[英]How can I undo input value by rolling back the previous valid value in Kendo UI

I have an input component as the following: 我有一个输入组件,如下所示:

<input type="number" value="0" name="weight" format="0" min="50" max="100">

What I expect: 我的期望:

  1. input a number between 50 and 100, for example 70; 输入介于50和100之间的数字,例如70;
  2. focus other component then focus back on this input; 关注其他组件,然后再关注此输入;
  3. input a number which is lower than 50 or greater than 100, for example 20; 输入一个小于50或大于100的数字,例如20;
  4. focus other component 关注其他组成部分
  5. I expect the box rolling back to 70 instead of populating 50, the min attribute. 我希望该框回滚到70,而不是填充50(即min属性)。

Thank you for your help 谢谢您的帮助

You can do something like: 您可以执行以下操作:

var ntb = $("#num").kendoNumericTextBox({
    min : 50,
    max : 100
}).data("kendoNumericTextBox");

var oldvalue = undefined;
$("#num").on("focusin", function(e) {
    oldvalue = $("#num").val();
});

$("#num").on("focusout", function(e) {
    var value = $("#num").val();
    if (value < ntb.options.min || value > ntb.options.max) {
        $("#num").val(oldvalue);
    }
});

Basically you intercept the focusin event and save previous value and in focusout you check that are inside the limits, if not, you restore the old saved value. 基本上,您会拦截focusin事件并保存以前的值,而在focusout检查是否在限制范围内,如果不是,则还原旧的保存值。

EDIT If you need to do the same for many input fields in the page, what you can do is: 编辑如果您需要对页面中的许多输入字段执行相同的操作,则可以执行以下操作:

// Save old value in the numeric text box by adding an oldvalue property to it
$("[data-role='numerictextbox']").on("focusin", function(e) {
    var me = $(this).data("kendoNumericTextBox");
    me.oldvalue = $(this).val();
});

// Restore saved old value if value is not in range
$("[data-role='numerictextbox']").on("focusout", function(e) {
    var value = $(this).val();
    var me = $(this).data("kendoNumericTextBox");
    if (value < me.options.min || value > me.options.max) {
        $(this).val(me.oldvalue);
    }
});

See it running here : http://jsfiddle.net/OnaBai/yYX7m 看到它在这里运行: http : //jsfiddle.net/OnaBai/yYX7m

There is a field named "_old" in the KendoNumericTextBox object; KendoNumericTextBox对象中有一个名为“ _old”的字段。 this is useful for this purpose, at least when handling a NumericTextBoxSpinEvent, viz. 这对于此目的很有用,至少在处理NumericTextBoxSpinEvent时有效 :

function spinEventHandler(e) {
    alert("delta from spin is: " + (+e.sender._value - e.sender._old));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果以前的输入具有有效值,我该如何显示输入字段? - How can I show input fields only when previous input has a valid value? 如何获取先前兄弟的输入的值? - How can I get the value of an input which is a previous sibling? 取消单击后如何找回原先的input值? - How can I get back a former value of input when unclicked? 当 go 后退时,如何获取上一个输入值 - When go to back, how to get previous input value 如何从kendo UI中的dataSource获取某些字段值 - How can get certain field value from dataSource in kendo UI kendo ui grid-防止在值无效的地方撤消 - kendo ui grid- prevent undoing where value is not valid 如何从Kendo UI模板获取价值 - How do i get a value from Kendo UI template 在kendo ui网格中执行批处理操作时,选定的kendo Dropdownlist值将重置为先前选择的值 - Selected kendo Dropdownlist value getting reset to previous selected value while performing batch operation in kendo ui grid 如何设置Kendo文本框的值并将该值传递给控制器​​,以便模型有效? - How do I set the value of a Kendo Text Box and pass that value to the controller so the model is valid? 如何使用类型范围输入中的值来设置jQuery UI Slider的值? - How can I use the value from an input of type range to set the value of a jQuery UI Slider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM