检查以下代码。在那里你会找到两张桌子。每个表<th>
首先包含主要类别名称,并包含所有子类别名称。
我的目标是当我点击主要类别文本时,选择类别选项将自动选择该类别,当我点击子类别文本时,它将只复制子类别文本并粘贴到搜索框。
$(document).ready(function() {
$(".table").each(function() {
$(this).find('tr').slice(7).hide();
});
$(".expendbtn").html("More");
$(document).on("click", '.expendbtn', function() {
if ($(this).text() == "More") {
$(this).prev(".table").find("tr").show();
$(this).html("Less");
} else {
$(this).prev(".table").find('tr').slice(7).hide();
$(this).html("More");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<div class="container">
<div class="input-group">
<input class="form-control txtSearch" id="searchString" type="text" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default btnGo" id="searchButton" type="button">Search</button>
</span>
</div>
<br/>
<select class="form-control" id="selectMainCategory">
<option selected="selected" value="">Select Category</option>
<option value="20081">Antiques</option>
<option value="550">Art</option>
<option value="2984">Baby</option>
<option value="267">Books</option>
<option value="12576">Business & Industrial</option>
<option value="625">Cameras & Photo</option>
<option value="15032">Cell Phones & Accessories</option>
<option value="11450">Clothing, Shoes & Accessories</option>
<option value="11116">Coins & Paper Money</option>
<option value="1">Collectibles</option>
<option value="58058">Computers/Tablets & Networking</option>
<option value="293">Consumer Electronics</option>
<option value="14339">Crafts</option>
<option value="237">Dolls & Bears</option>
<option value="11232">DVDs & Movies</option>
<option value="45100">Entertainment Memorabilia</option>
<option value="172008">Gift Cards & Coupons</option>
<option value="26395">Health & Beauty</option>
<option value="11700">Home & Garden</option>
<option value="281">Jewelry & Watches</option>
<option value="11233">Music</option>
<option value="619">Musical Instruments & Gear</option>
<option value="1281">Pet Supplies</option>
<option value="870">Pottery & Glass</option>
<option value="10542">Real Estate</option>
<option value="316">Specialty Services</option>
<option value="888">Sporting Goods</option>
<option value="64482">Sports Mem, Cards & Fan Shop</option>
<option value="260">Stamps</option>
<option value="1305">Tickets & Experiences</option>
<option value="220">Toys & Hobbies</option>
<option value="3252">Travel</option>
<option value="1249">Video Games & Consoles</option>
<option value="99">Everything Else</option>
</select>
<br/>
<div class="col-lg-3">
<table class="table table-striped jambo_table">
<thead>
<tr class="headings">
<th>
<h4>Business & Industrial</h4>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adhesives, Sealants & Tapes</td>
</tr>
<tr>
<td>Agriculture & Forestry</td>
</tr>
<tr>
<td>Automation, Motors & Drives</td>
</tr>
<tr>
<td>Cleaning & Janitorial Supplies</td>
</tr>
<tr>
<td>Construction</td>
</tr>
<tr>
<td>Electrical & Test Equipment</td>
</tr>
<tr style="display: none;">
<td>Facility Maintenance & Safety</td>
</tr>
<tr style="display: none;">
<td>Fasteners & Hardware</td>
</tr>
<tr style="display: none;">
<td>Fuel & Energy</td>
</tr>
<tr style="display: none;">
<td>Healthcare, Lab & Life Science</td>
</tr>
<tr style="display: none;">
<td>Heavy Equipment</td>
</tr>
<tr style="display: none;">
<td>Heavy Equipment Attachments</td>
</tr>
<tr style="display: none;">
<td>Heavy Equipment Parts & Accs</td>
</tr>
<tr style="display: none;">
<td>HVAC</td>
</tr>
<tr style="display: none;">
<td>Hydraulics, Pneumatics & Pumps</td>
</tr>
<tr style="display: none;">
<td>Light Equipment & Tools</td>
</tr>
<tr style="display: none;">
<td>Manufacturing & Metalworking</td>
</tr>
<tr style="display: none;">
<td>Material Handling</td>
</tr>
<tr style="display: none;">
<td>Office</td>
</tr>
<tr style="display: none;">
<td>Printing & Graphic Arts</td>
</tr>
<tr style="display: none;">
<td>Restaurant & Catering</td>
</tr>
<tr style="display: none;">
<td>Retail & Services</td>
</tr>
<tr style="display: none;">
<td>Websites & Businesses for Sale</td>
</tr>
<tr style="display: none;">
<td>Other Business & Industrial</td>
</tr>
</tbody>
</table>
<button class="btn btn-info expendbtn" type="button">More</button>
</div>
<div class="col-lg-3">
<table class="table table-striped jambo_table">
<thead>
<tr class="headings">
<th>
<h4>Cameras & Photo</h4>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Binoculars & Telescopes</td>
</tr>
<tr>
<td>Camcorders</td>
</tr>
<tr>
<td>Camera & Photo Accessories</td>
</tr>
<tr>
<td>Camera Drones</td>
</tr>
<tr>
<td>Camera Drone Parts & Accs</td>
</tr>
<tr>
<td>Camera Manuals & Guides</td>
</tr>
<tr style="display: none;">
<td>Digital Cameras</td>
</tr>
<tr style="display: none;">
<td>Digital Photo Frames</td>
</tr>
<tr style="display: none;">
<td>Film Photography</td>
</tr>
<tr style="display: none;">
<td>Flashes & Flash Accessories</td>
</tr>
<tr style="display: none;">
<td>Lenses & Filters</td>
</tr>
<tr style="display: none;">
<td>Lighting & Studio</td>
</tr>
<tr style="display: none;">
<td>Replacement Parts & Tools</td>
</tr>
<tr style="display: none;">
<td>Tripods & Supports</td>
</tr>
<tr style="display: none;">
<td>Video Production & Editing</td>
</tr>
<tr style="display: none;">
<td>Vintage Movie & Photography</td>
</tr>
<tr style="display: none;">
<td>Wholesale Lots</td>
</tr>
<tr style="display: none;">
<td>Other Cameras & Photo</td>
</tr>
</tbody>
</table>
<button class="btn btn-info expendbtn" type="button">More</button>
</div>
</div>
答案 0 :(得分:0)
此脚本将选择带有标题文本的选项,并将td文本复制为搜索字符串。
$('th').on('click', function () {
var txt =$.trim( $(this).text());
console.log(txt);
$("#selectMainCategory option").each(function () {
if ( $.trim($(this).text()) === txt) {
$(this).attr("selected", "selected");
return;
}
});
});
$('td').on('click', function () {
var txt =$.trim( $(this).text());
console.log(txt);
$("#searchString").val(txt);
});