【发布时间】:2018-10-02 15:38:45
【问题描述】:
我有一个 JavaScript 练习,我试图在单击特定链接后显示一个包含数据的表格。这实际上是一个 3 部分的练习。 2部分完成。只有我不能做的部分是桌子部分。这是一个下拉选择菜单,用户可以在其中选择一个类别。一旦选择了类别,就会出现该类别中的业务。每个业务都是一个li标签和锚链接。单击业务链接后,它会跳转到页面并显示一个包含该特定业务信息的表格。用数据显示表格是我的问题。有人可以在不更改我已有的代码的情况下帮助我吗?
var businessListings = {
hair: {
"Tina's Hair Salon": {
Address: "1520 Lakeview Drive Chester, Pa 19013",
PhoneNumber: "610-872-1109",
Website: "www.tinahair.com",
Specialty: "All hair types"
}, // line ends Tina Hair Salon info
"Dollhouse Hair Studio": {
Address: "485 4th Street Chester, Pa 19013",
PhoneNumber: "484-419-8595",
Website: "www.dhhairstudio.com",
Specialty: "Braiding, All hair types"
} // line ends dollhouse info
}, // line ends hair category
healBeauty: {
Myra 's Make-Up Kit": {
Address: "4260 Jeffrey Lane Chester, Pa 19013",
PhoneNumber: "610-848-2014",
Website: "www.mymakeupkit.com",
Specialty: "Make all skin tones, ages, events"
}, // line ends Myra's makeup info
"Purity Spa": {
Address: "213 W. 24th Street Chester, Pa 19013",
PhoneNumber: "267-608-4752",
Website: "www.purityspa.com",
Specialty: "Universal"
} // line ends Purity spa info
}, // line ends healBeauty category
food: {
"Larry's Pies": {
Address: "250 3rd Street Chester, Pa 19013",
PhoneNumber: "610-724-2008",
Website: "N/A",
Specialty: "Pies, Desserts"
}, // line ends Larry's Pies info
"C-Pride Super Market": {
Address: "3018 W. 7th Street Chester, Pa 19013",
PhoneNumber: "610-803-2020",
Website: "www.cpridemarket.com",
Specialty: "All Foods, Groceries"
} // line ends C-Pride Market info
}, // line ends food category
travel: {
"G&W Travel Agency": {
Address: "200 Avenue of States Chester, Pa 19013",
PhoneNumber: "484-203-8587",
Website: "www.gandwtravel.com",
Specialty: "Traveling, All Occasions"
}, // line ends G&W Travel info
"Grant Travel Agency": {
Address: "1834 Edward Street Chester, Pa 19013",
PhoneNumber: "267-515-7062",
Website: "N/A",
Specialty: "Traveling, All Occasions"
} // line ends Grant Travel info
}, // line ends travel category
fitness: {
"Elijah's Supreme Gym": {
Address: "420 Tilghman Street Chester, Pa 19013",
PhoneNumber: "610-898-5040",
Website: "www.esupremegym.com",
Specialty: "Fitness, All Body Types"
}, // line ends Elijah's Gym info
"Fitness Girl Gym": {
Address: "2790 E. 15th Street Chester, Pa 19013",
PhoneNumber: "267-762-8240",
Website: "www.esupremegym.com",
Specialty: "Fitness, All Body Types"
} // line ends Fitness Gym info
},
retCloth: {
"Enid and 4 Girls Boutique": {
Address: "2200 Edgmont Avenue Chester, Pa 19013",
PhoneNumber: "610-203-8570",
Website: "www.enidand4girls.com",
Specialty: "Clothing, Accessories"
}, // line ends Enid and 4 Girls info
"Fatima & Iman's Closet": {
Address: "824 Parker Street Chester, Pa 19013",
PhoneNumber: "610-800-1937",
Website: "www.fatimaandiman.com",
Specialty: "Clothing, Accessories"
} // line ends Enid and 4 Girls info
}, // line ends retCloth info
auto: {
"Arthur's": {
Address: "1019 Edgmont Avenue Chester, Pa 19013",
PhoneNumber: "484-626-1149",
Website: "N/A",
Specialty: "All Vehicle Issues"
}, // line ends Arthur's info
"Johnson and Son Mechanics": {
Address: "517 Pennell Street Chester, Pa 19013",
PhoneNumber: "610-504-8127",
Website: "www.johnsonandsons.com",
Specialty: "Auto Body Work"
} // line ends Johnson and Sons info
}, // line ends auto info
homeDec: {
"Charisma Home Decor": {
Address: "7790 Melrose Avenue Chester, Pa 19013",
PhoneNumber: "610-203-9045",
Website: "www.charismahomedecor.com",
Specialty: "Handcrafted Home Decor, Elegant Home Decor, All Styles"
}, // line ends Charisma Home Decor info
"Lily's Home Decor": {
Address: "1402 Ward Road Chester, Pa 19013",
PhoneNumber: "267-761-2990",
Website: "www.lilydesign.com",
Specialty: "All Styles"
} // line ends Lily Home Decor info
}, // line ends homeDec info
photo: {
"Marple Photography": {
Address: "618 Taylor Avenue Chester, Pa 19013",
PhoneNumber: "610-499-8615",
Website: "www.marplephotography.com",
Specialty: "All Styles, Occasions"
}, // line ends Marple Photography info
"Kyle's Photography": {
Address: "216 W. 9th Street Chester, Pa 19013",
PhoneNumber: "484-223-7831",
Website: "N/A",
Specialty: "Professional Shoots, All Styles, Occasions"
} // line ends Kyle's Photography info
}, // line ends photo info
musEnt: {
"Mark's Studios": {
Address: "2407 Linden Street Chester, Pa 19013",
PhoneNumber: "267-808-4256",
Website: "www.markstudios.com",
Specialty: "R&B, Hip Hop"
}, // line ends Mark's Studios info
"CC Moore Recording Studio": {
Address: "1420 Melrose Avenue Chester, Pa 19013",
PhoneNumber: "267-615-2009",
Website: "www.ccmoorestudio.com",
Specialty: "All Genres"
} // line ends CC Moore Recording info
}, // line ends musEnt category paint: {
"Ulysses's Paint": {
Address: "2201 Edgmont Avenue Chester Pa 19013",
PhoneNumber: "267-298-0845",
Website: "www.ulyssespaint.com",
Specialty: "Mixing Paint, Selling All Paint Types"
}, // line ends Ulysses's Paint info
"Dock Paint Supplies": {
Address: "1007 W. 7th Street Chester, Pa 19013",
PhoneNumber: "484-318-5190",
Website: "N/A",
Specialty: "All Paint Type Supplies"
} // line ends Mark's Studios info
}, // line ends paint category info
}; // line ends "multi-dimensional" array object
var businessListNames = [];
var selectedCategory;
var textNameOfBusinessSelected;
var nameOfBusinessesUnderCategory;
function businessListingResults(databaseList) {
// function is for when a category is selected
selectedCategory = $("#select-business-listing option:selected").val();
textNameOfBusinessSelected = $("#select-business-listing
option: selected ").text();
nameOfBusinessesUnderCategory = databaseList[selectedCategory];
for (findBusinessUnderCategory in nameOfBusinessesUnderCategory) {
businessListNames.push(findBusinessUnderCategory);
console.log(findBusinessUnderCategory);
} // line ends for in loop
console.log("ice cream"); $("#listingResults").html("Business Listings Under Category <strong>" +
textNameOfBusinessSelected + "</strong> are: <br/><br/><ul
id = 'listOfBusinesses' > < li > < a href = 'javascript-exercise-16.html#bottomOfPage'
onclick = 'clickListingLink()' > " + businessListNames.join(" < br / > < a href = 'javascript-exercise-16.html#bottomOfPage'
onclick = 'clickListingLink()' >
<
li > ") + " < /li></ul > ");
} // line closes function
function clickListingLink() {
// function is for when a link is clicked after list of businesses appear
$('#listOfBusinesses').on('click', 'li', function() {
clickListingLink();
console.log('you clicked me');
seeListingInfoTable();
$("hr").show();
});
}
function seeListingInfoTable(titleOfBusinessSelected, businessLinkSelected) {
// function is for table that shows info of specific link clicked
console.log("apple");
var tbl = "";
tbl += '<table class="table table-hover">';
tbl += '</table>';
tbl += '<tbody>';
tbl += '<caption class="listingTitle">' + titleOfBusinessSelected +
'</caption>';
tbl += '<tr>';
tbl += '<th>Address</th>';
tbl += '<th>Phone Number</th>';
tbl += '<th>Website</th>';
tbl += '<th>Specialty</th>';
tbl += '</tr>';
tbl += '<tr>';
tbl += '<td><div class="row_data" edit_type="click" col_name="fname">' +
businessLinkSelected["Address"] + '</div></td>';
tbl += '<td><div class="row_data" edit_type="click" col_name="fname">' +
businessLinkSelected["Phone Number"] + '</div></td>';
tbl += '<td><div class="row_data" edit_type="click" col_name="fname">' +
businessLinkSelected["Website"] + '</div></td>';
tbl += '<td><div class="row_data" edit_type="click" col_name="fname">' +
businessLinkSelected["Specialty"] + '</div></td>';
tbl += '</tr>';
tbl += '</tbody>';
tbl += '</table>';
$(document).find("#infoTable").html(tbl);
} // line ends seeListingInfoTable function
<h4 class="title">Select a Category from the drop down menu to check your local businesses</h4>
<br/>
<select id="select-business-listing" name="submitted[select_your_program]" class="select-a-business">
<option value="" selected="selected">- Select a business category -</option>
<option value="hair">Hair</option>
<option value="healBeauty">Health and Beauty</option>
<option value="food">Food</option>
<option value="travel">Travel</option>
<option value="fitness">Fitness</option>
<option value="retCloth">Retail and Clothing</option>
<option value="auto">Automotive</option>
<option value="homeDec">Home Decor</option>
<option value="photo">Photography</option>
<option value="musEnt">Music and Entertainment</option>
<option value="paint">Paint</option>
</select>
<br/>
<br/>
<br/>
<button class="listingsBtn" onclick="businessListingResults(businessListings)">Search Listings</button>
</div>
<p id="listingResults"></p>
<br/>
<br/>
<hr/>
<br/>
<div id="bottomOfPage">
<div id="infoTable">
</div>
【问题讨论】:
-
请将您的代码放入 CSS/HTML/JS 代码块...或类似jsfiddle.net的网站上
-
这不是最小的,但我至少将代码移到了他们的 sn-p 中并打好了,所以它更易于管理。
-
好的,我很抱歉格式化。我现在就修。
-
@Elias 这是我的小提琴链接> jsfiddle.net/Angel91/pe8vuq2f
标签: javascript jquery html arrayobject