[英]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: 我的期望:
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.