我有一个JavaScript练习,您可以通过3种不同的方式来搜索公司列表。我试图做一个if语句,说如果第一个选项等于1或更高(表示不为空),而其他2个搜索选项设置为0(表示为空),则发生某些情况。但是,代码没有运行我的if语句。我知道这是因为我检查了控制台。我的if语句怎么了?
JS
<script>
var listOfBusinesses = {
"Food": "Leroy's Restaurant",
"Hair": "Angie's Hair Salon",
"Beauty": "Nora - MakeUp Artist",
"Hardware": "Harry and Blu",
"Food": "Elijah's Market",
"Beauty": "Flair Nail Salon",
"Retail": "Tiffany's Boutique",
"Retail": "Charisma Home Decor",
"Hair": "Lady Luck Salon",
"Food": "Charlie's Produce"
};
function getBusinessResults(databaseOfBusinesses) {
var searchByName = $("#inputByName").val();
console.log(searchByName);
var searchByFirstL = $("#inputFirstLetter").val();
console.log(searchByFirstL);
var searchByCategory = $("#inputByCategory").val();
console.log(searchByCategory);
event.preventDefault();
if (searchByName > 0 && searchByFirstL == 0 && searchByCategory
== 0) {
console.log("apple");
for (i = 0; i < databaseOfBusinesses.length; i++) {
if (searchByName == databaseOfBusinesses[i]) {
console.log("apple");
$("#businessResults").html("Yes " + searchByName + "is a
business listed with us.");
} // line closes if statement
} // line closes for loop
} // line closes if statement
} // line closes function
</script>
HTML:
<form action="javascript-exercise-15.html" method="post">
<p id="byName">Search by Business Name</p>
Business Name: <input id="inputByName" type="text"
name="businessName" value="">
<br/><br/>
<p id="byFirstL">Search by 1st Letter of Business Name</p>
First Letter of Business Name: <input id="inputFirstLetter"
type="text" name="firstLetter" value="">
<br/><br/>
<p id="byCategory">Search by Category of Business</p>
Category of Business: <input id="inputByCategory" type="text"
name="businessCategory" value="">
<br/><br/></br/>
<button class="businessBtn"
onclick="getBusinessResults(listOfBusinesses)">Search
Business</button>
</form>
<p id="businessResults"></p>