2

我有一个包含 a 的表单,Repeater其中包含 aRadioButtonList和 a TextBox。当用户更改其中一个 TextBoxes 中的单选按钮或文本时,该 Repeater 项目的选定项目和关联文本应保存到数据库中。

由于该.change()功能,当单选按钮更改时保存很容易,但是对于TextBox我正在创建一个计时器,当文本更改时保存数据 2 秒后用户停止键入

问题是,如果用户在计时器执行之前离开页面或开始输入不同的文本框,我需要手动执行计时器。我怎样才能做到这一点?

我的 javascript 看起来像这样:

var typingTimer;                //timer identifier
var doneTypingInterval = 2000;  //time in ms, 2 second for example

$('textarea').on('input', function () {
    // Clear timer
    clearTimeout(typingTimer);

    // Set new timer to save form, passing current repeater item to Save method
    var parent = $(this).closest('.my-repeater-section');
    typingTimer = setTimeout(function () { saveForm(parent); }, doneTypingInterval);
});
4

2 回答 2

2

计时器只是一个延迟的函数调用。所以,命名你的函数,你就可以“手动”调用它:

var typingTimer;                //timer identifier
var doneTypingInterval = 2000;  //time in ms, 2 second for example

$('textarea').on('input', function () {
    // Clear timer
    clearTimeout(typingTimer);

    // Set new timer to save form, passing current repeater item to Save method
    var parent = $(this).closest('.my-repeater-section');

    function callback() {
        saveForm(parent);
    }

    typingTimer = setTimeout(callback, doneTypingInterval);

    // call it manually like usual:
    // callback();
});
于 2012-12-10T16:22:02.947 回答
2

在模糊上调用您的保存功能,这样当用户离开该文本区域时,它将失去焦点,如果您收听该事件,您可以在那里使用您的保存功能

 $("textarea").blur(function(){
  //Call your save function here
});
于 2012-12-10T16:25:05.367 回答