代码之家  ›  专栏  ›  技术社区  ›  Maksim Vi.

jQueryUI选项卡选择和验证

  •  0
  • Maksim Vi.  · 技术社区  · 15 年前

    在切换到下一个选项卡之前,我尝试验证选项卡内容(使用Ajax验证)。因此,当用户单击选项卡时,当前选项卡内容将发送到服务器进行验证。当收到服务器的结果时,我切换到下一个选项卡。下面是一些代码:

    $('#tabs').tabs({
        select: function(event, ui) {
            ...
            validate(currentIndex, nextIndex);
            return false;
        }
    });
    
    function validate(currentIndex, nextIndex){
        $.ajax({
            ...
            complete: function(){
                $("#tabs").tabs('select', nextIndex);
            }
            ...
        }
    }
    

    您可能已经看到了问题,它是无限循环,因为验证会导致选项卡的选择处理程序导致验证,等等。如果没有全局变量,我如何解决这个问题?

    谢谢。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Riki    15 年前
        2
  •  3
  •   Janusz Slota    15 年前

    this - official Jquery tabs documentation

    $('#tabs').tabs(
    {
        select: function(event, ui)
        {
            ...
            return validate(currentIndex, nextIndex);
        }
    });
    
    function validate(currentIndex, nextIndex)
    {
        $.ajax(
        {
            ...
            success: function(data)
            {
                // example response: {"error": 0}
                if(!data.error) return true;
                else return false;
            }
            ...
        }
    }