Does someone know how to do an if
statement when I want to specify that I want the code happen only if there are results for the search?
For example when user searches for a pie recipe and there are results then the if
should be evoked. But when user searches something that doesn't get any results then else
should be done.
var searchword = $('#search').val(); //search is id for the input field
if (searchword ...? ) {
// there's a hit for the search and code continues
}
else {
// there aren't any hits so this code continues
}
I first tried to use if (searchword > 0) { }
but that's not really doing what I want it to do. So the user should be able to write on the input field but if there are search results then the if
should be done and if not then the else
.
Here's the hole part of the code if it helps to understand:
function () {
var searchword = $('#search').val();
if ( // statement here ) {
$.ajax({
url: "http://ift.tt/2qmmzkn",
dataType: "jsonp",
jsonp: "getLivsMedel",
data: {
callback: 'getLivsMedel',
namn: searchword
}
});
} else {
$("tbody").empty();
}
}
Aucun commentaire:
Enregistrer un commentaire