[英]How can I undo input value by rolling back the previous valid value in Kendo UI
我有一個輸入組件,如下所示:
<input type="number" value="0" name="weight" format="0" min="50" max="100">
我的期望:
謝謝您的幫助
您可以執行以下操作:
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);
}
});
基本上,您會攔截focusin
事件並保存以前的值,而在focusout
檢查是否在限制范圍內,如果不是,則還原舊的保存值。
編輯如果您需要對頁面中的許多輸入字段執行相同的操作,則可以執行以下操作:
// 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);
}
});
看到它在這里運行: http : //jsfiddle.net/OnaBai/yYX7m
KendoNumericTextBox對象中有一個名為“ _old”的字段。 這對於此目的很有用,至少在處理NumericTextBoxSpinEvent時有效。 :
function spinEventHandler(e) {
alert("delta from spin is: " + (+e.sender._value - e.sender._old));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.