我想在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
});
答案 0 :(得分:1)
您的代码存在问题:
close: function(event,ui){
count = 0;
$('#count').text(count);
}
每次关闭自动完成时 this will make count = 0
当你提交表单时,count var将始终为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
});
答案 1 :(得分: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;
});