简体   繁体   English

根据下拉选择用.load填充div

[英]Populate div with .load based on drop down selection

Alright this feels like a basic thing, but im struggling to make it work - What I'm trying to accomplish is a drop down list with about 45 county's, when one is selected, an empty div in the body will load with a div from another html page (where I'm going to house the 45 divs of corresponding location information). 好吧,这听起来像是一件基本的事情,但是我很难做到这一点-我要完成的工作是在下拉列表中列出约45个县,当选中一个县时,主体中的一个空div将加载来自另一个html页面(我将在其中放置45 div的相应位置信息)。

Intending on using an ajax load style event 打算使用ajax加载样式事件

<script>
$(document).ready(function(){
        $("#county-result").load("county_list.html #county1");
});
</script>

But to make the script less heavy I would want the dropdown value to be its matching county div id to populate that part of the load function (rather then writing 45 individual ones) 但是,为了使脚本不那么沉重,我希望下拉列表值为与其匹配的County div id,以填充部分加载函数(而不是编写45个独立的div)

Ideas on how I can do this? 关于如何执行此操作的想法?

I created a plunkr for you! 我为您打造了一个小矮人 I hope it helps 希望对您有所帮助

What I'm basically doing here is, I'm adding a change event listener to a select (dropdown) And I make sure my divs in the countries html file have the same id's as my option values in my main file (The one where you will have the dropdown menu) 我在这里基本上要做的是,将一个更改事件侦听器添加到一个select(下拉列表)中,并且确保我在国家(地区)html文件中的div与我的主文件中的选项值具有相同的ID(您将拥有下拉菜单)

So if you want information about germany you have to make sure a countries option and div will look a bit like this 因此,如果您想获得有关德国的信息,则必须确保有一个country选项,并且div看起来会像这样

<option value="germany">germany</option>

<div id="germany">
    <h1>Berlin</h1>
</div>

This question is a bit broad. 这个问题有点广泛。 But, if I needed to work with information related to 45 counties and needed to display the info for the county selected from a dropdown, I'd use JSON as my data source and populate the divs using a template and iterating over the JSON object and looking for the selected id. 但是,如果我需要处理与45个县相关的信息,并且需要显示从下拉列表中选择的县的信息,则可以使用JSON作为数据源,并使用模板填充div并遍历JSON对象,寻找所选的ID。

The below is an example of how that might work. 下面是一个示例,说明如何工作。 Note that I'm actually building the select box options dynamically based on the dataset itself and the setup allows you to easily update the data if needed. 请注意,我实际上是根据数据集本身动态构建选择框选项,并且该设置使您可以根据需要轻松地更新数据。

Note how you get the JSON is up to you. 请注意,如何获取JSON取决于您。 I have hard coded it for this example but you could get it via an ajax request or using .get() , .load() , etc.. 在本示例中,我已经对其进行了硬编码,但是您可以通过ajax请求或使用.get() .load()等获取它。

 var myCountyInfo = { counties: [{ name: 'County 1', id:123, locationInfo: { lat: 453245, lng: 45545, avgTemp: '75f', population: '5 B.' } }, { name: 'County 2', id:456, locationInfo: { lat: 11221, lng: 542222, avgTemp: '59f', population: '2 B.' } }, { name: 'County 3', id:789, locationInfo: { lat: 88555, lng: 54757, avgTemp: '58f', population: '1 B.' } }] } function populateSelectBoxes($select, data) { var counties = []; $.each(data, function() { counties.push('<option value="'+this.id+'">' + this.name + '</option>'); }); $select.append(counties.join('')); } function populateTableRow($tableBody, data, selectedCountyId) { var county; $.each(data, function() { if (this.id == selectedCountyId) { county = this; return false; } }); $tableBody.html('<tr>'+ '<td>' + county.name + '</td>'+ '<td>' + county.locationInfo.lat +'</td>'+ '<td>' + county.locationInfo.lng + '</td>'+ '<td>' + county.locationInfo.avgTemp + '</td>'+ '<td>' + county.locationInfo.population + '</td>'+ '</tr>'); } populateSelectBoxes($('#my-select'), myCountyInfo.counties); $('#my-select').change(function() { var $this = $(this); var selection = $this.val(); populateTableRow($('#my-table tbody'), myCountyInfo.counties, selection); }); 
 td,th{ padding:5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="" id="my-select"></select> <table id="my-table" border="1"> <thead> <tr> <th>County</th> <th>Lat.</th> <th>Lng.</th> <th>Avg Temp</th> <th>Population</th> </tr> </thead> <tbody> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM