0

当 jQuery 自动完成没有找到结果时,我想禁用我的提交。我现在工作一半,我计算我的结果很好。

唯一的问题是当我有结果并且我提交时,结果会弹回 0,所以我无法提交。我想在提交时记住自动完成计数值。

我计算我的结果:

$('#stock_input').autocomplete({

        source:'autocomplete.php',
        autoFocus: true,
        minLength:1,
    open: function(event,ui){
            count = $(this).autocomplete("widget").find( "li" ).length;
            $('#count').text(count);
    },
    close: function(event,ui){
            count = 0;
            $('#count').text(count);
    }
});

允许提交设置:

    var allowSubmit = true;
        $("#updatestock_form").submit(function(e){
            if(count === 0){ allowSubmit = false; alert(count); } 
            else { allowSubmit = true; alert(count); }
            if (!allowSubmit) return false;

        //rest of submit code
        });
4

2 回答 2

1

您的代码的问题:

close: function(event,ui){
            count = 0;
            $('#count').text(count);
    }

this will make count = 0每次关闭自动完成

当您提交表单时,计数变量将始终为 0,这使得allowSubmit = false

 $("#updatestock_form").submit(function(e){
            if(count === 0){ allowSubmit = false; alert(count); } 
            else { allowSubmit = true; alert(count); }
            if (!allowSubmit) return false;

        //rest of submit code
        });

您可以在提交时检查这一点

$("#updatestock_form").submit(function(e){
            var count = $('#stock_input').autocomplete("widget").find( "li" ).length;
            if(count === 0){ allowSubmit = false; alert(count); } 
            else { allowSubmit = true; alert(count); }
            if (!allowSubmit) return false;

        //rest of submit code
        });
于 2013-04-14T09:26:27.850 回答
0

我用下面的代码做到了:

$('#stock_input').autocomplete({
        source:'autocomplete.php',
        autoFocus: true,
        minLength:1,
        response: function(event, ui) {
            if (ui.content.length === 0) {
                count_result = 0; //no results
            } else {
                count_result = 1;
            }
        }
});

和:

$("#updatestock_form").submit(function(e){
    if(count_result === 0){ allowSubmit = false; }
        else { allowSubmit = true; }
        if (!allowSubmit) return false;
});
于 2013-04-14T10:04:46.787 回答