I have a jquery loop where I am slicing the json array upto 5 and now I need to check whether the key value is empty or not. Once I check for 5 elements,if my condition works then it will print 5 data but what if it doesn't? It shows me same amount of time the else.
Here is my jquery code:
$.each(data.slice(0,5), function(key, value) {
if(value.emailID!="")
{
$("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
$("#loader_gif").hide();
}
else
{
$("#loader_gif").hide();
$("#search-btn").removeAttr("disabled");
$("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message');
}
});
If my emailID is blank for all array elements, then it appends for 5 times with message "No records found" any solution to that?
return false; = break;
return true; or return; = continue;
From Jquery Docs http://api.jquery.com/jQuery.each/
let found = false;
$.each(data.slice(0,5), function(key, value) {
if(value.emailID!="")
{
$("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
$("#loader_gif").hide();
found = true;
}
});
if(!found){
$("#loader_gif").hide();
$("#search-btn").removeAttr("disabled");
$("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message');
}
in my case, i think,
var valid = false;
$.each(data.slice(0,5), function(key, value) {
if(value.emailID!="") {
$("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
$("#loader_gif").hide();
valid = true;
}
});
if (!valid) {
$("#loader_gif").hide();
$("#search-btn").removeAttr("disabled");
$("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message');
}
I hope It's what you want.
var hasData =false
$.each(data.slice(0,5), function(key, value) {
if(value.emailID!="")
{
hasData = true
$("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
$("#loader_gif").hide();
}
});
if(!hasData){
$("#loader_gif").hide();
$("#search-btn").removeAttr("disabled");
$("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></enter code herep>").addClass('before-search-message');}
This is probably what you're looking for. You were iterating over display no results 5 times
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.