我有一个输入,我想检查它是否为空。我使用维基百科的API来获取信息,这是AJAX部分:检查元素值是否为空或与Jquery
$.ajax({
type: 'GET',
url: searchedUrl,
async: false,
dataType: 'json',
success: function(data){
var length = data[1].length;
var inputValue = $('#searchInput').val();
console.log(inputValue);
if (!$('#searchInput').val()){
error = true;
console.log('err');
} else {
if (error = true){
$('#errorMsg').html('Search error').fadeOut();
}
$('.title').css('margin-top', '30px');
$('#output').removeClass('hidden');
for (var i = 0; i < length; i++){
$('#output').prepend('<li><a href='+data[3][i]+'>'+data[1][i]+'</a><br>'+data[2][i]+'</li>');
}
}
},
error: function(err){
error = true;
$('#errorMsg').html('Search error').fadeIn();
}
});
然而,当它成功,我希望它检查输入的值是否为空。我使用IF条件,但它不会在控制台中写入'err'。这就像它忽略了一切,如果值是空的。我也试过这样的条件:
if (inputValue !== ''){
// Error;
}
但它不工作。
PS:这是我的jsfiddle:https://jsfiddle.net/dietic/sdfu967y/
谢谢您的帮助!
'if(inputValue.length){...}'!如果长度为'0',那么'if'主体将不会被执行。 –
你为什么不放一个调试器,看看那个时候有什么价值? –
值是“”但它仍然没有任何作用。 –