[英]Using jquery to show/hide several table rows
I am trying to create a table of periods and prices. 我正在尝试创建周期表和价格表。
The table header includes a select to change currencies. 表格标题包含用于更改币种的选择。 The table rows below this shows different periods and prices.
下面的表格行显示了不同的期间和价格。 The prices can be shown according to the currency selected in select.
可以根据在select中选择的货币显示价格。
I am new to jQuery, but have been fiddling around and looked through many posts here, a lot of which have helped me to get this far! 我是jQuery的新手,但是一直在这里闲逛并浏览许多帖子,其中许多内容都帮助我实现了这一目标!
But, I have now added a second row to my table and the price only changes in the first row, not the second. 但是,现在我在表中添加了第二行,并且价格仅在第一行中发生了变化,而在第二行中没有发生变化。
I appreciate any help you guys can give. 感谢您提供的任何帮助。
Javascript: Javascript:
<script type="text/javascript">
function currencyCheck()
{
var option = document.getElementById("currency-select").value;
if (option == "EURO") {
document.getElementById("EUR").style.display ="";
document.getElementById("GBP").style.display ="none";
document.getElementById("DKK").style.display ="none";
}
if (option == "GBPOUND") {
document.getElementById("GBP").style.display ="";
document.getElementById("EUR").style.display ="none";
document.getElementById("DKK").style.display ="none";
}
if (option == "DKKRONER") {
document.getElementById("DKK").style.display ="";
document.getElementById("EUR").style.display ="none";
document.getElementById("GBP").style.display ="none";
}
}
</script>
HTML: HTML:
<table class="rates">
<thead>
<tr>
<th>
Period
</th>
<th>
<select style="width:100px;" id="currency-select" onchange="currencyCheck()">
<option selected="selected" value="EURO">EUR</option>
<option value="GBPOUND">GBP</option>
<option value="DKKRONER">DKK</option>
</select>
/Week
</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="dates">
January
</td>
<td>
<div class="cost">
<div id="EUR">
€500
</div>
<div id="GBP" style="display:none">
£350
</div>
<div id="DKK" style="display:none">
DKK 1500
</div>
</div>
</td>
</tr>
<tr class="even">
<td class="dates">
February
</td>
<td>
<div class="cost">
<div id="EUR">
€500
</div>
<div id="GBP" style="display:none">
£350
</div>
<div id="DKK" style="display:none">
DKK 1500
</div>
</div>
</td>
</tr>
</tbody>
</table>
Ids are supposed to be unique identifiers, so document.getElementById()
, is only returning the first instance since their should only be one. id应该是唯一的标识符,所以
document.getElementById()
仅返回第一个实例,因为它们只能是一个。 Change the ids to a class, and use document.getElementByClassName()
instead. 将id更改为一个类,并改用
document.getElementByClassName()
。
Because you are changing the element with id "GBP", or so. 因为您正在更改ID为“ GBP”等的元素。 By using
getElementById
you are only changing one elements behaviour. 通过使用
getElementById
您仅更改一种元素的行为。 You should assign a class to all elements like: 您应该为所有元素分配一个类,例如:
<div class="GBP">£350</div>
After that you get all elements with class name GBP and you loop through them doing whatever you want: 之后,您将获得所有类名称为GBP的元素,并通过它们循环进行所需的操作:
var elems = document.getElementsByClassName('GBP');
for(var i=0; i < elems.length; i++) {
elems[i].style.display='block'
}
<table class="rates">
<thead>
<tr>
<th>Period</th>
<th>
<select style="width:100px;" id="currency-select" onChange="currencyCheck()">
<option selected="selected" value="EURO">EUR</option>
<option value="GBPOUND">GBP</option>
<option value="DKKRONER">DKK</option>
</select> /Week</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="dates">January</td>
<td>
<div class="cost">
<div class="EUR">€500</div>
<div class="GBP" style="display:none">£350</div>
<div class="DKK" style="display:none">DKK 1500</div>
</div>
</td>
</tr>
<tr class="even">
<td class="dates">February</td>
<td>
<div class="cost">
<div class="EUR">€500</div>
<div class="GBP" style="display:none">£350</div>
<div class="DKK" style="display:none">DKK 1500</div>
</div>
</td>
</tr>
</tbody>
</table>
function currencyCheck() {
var option = document.getElementById("currency-select").value;
var elems1 = document.getElementsByClassName('EUR');
for (var i = 0; i < elems1.length; i++) {
elems1[i].style.display = (option == 'EURO' ? 'block' : 'none');
}
var elems2 = document.getElementsByClassName('GBP');
for (var i = 0; i < elems2.length; i++) {
elems2[i].style.display = (option == 'GBPOUND' ? 'block' : 'none');
}
var elems3 = document.getElementsByClassName('DKK');
for (var i = 0; i < elems3.length; i++) {
elems3[i].style.display = (option == 'DKKRONER' ? 'block' : 'none');
}
}
A better solution would be to use the same class names for the cost
divs as the option
values, and then use this : 更好的解决方案是为
cost
div使用相同的类名作为option
值,然后使用以下方法:
jQuery : jQuery的:
function currencyCheck() {
var option = $("#currency-select").val();
$('.cost>div').hide();
$('.' + option).show();
}
HTML : HTML:
<table class="rates">
<thead>
<tr>
<th>Period</th>
<th>
<select style="width:100px;" id="currency-select" onchange="currencyCheck()">
<option selected="selected" value="EURO">EUR</option>
<option value="GBPOUND">GBP</option>
<option value="DKKRONER">DKK</option>
</select> /Week</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="dates">January</td>
<td>
<div class="cost">
<div class="EURO">€500</div>
<div class="GBPOUND" style="display:none">£350</div>
<div class="DKKRONER" style="display:none">DKK 1500</div>
</div>
</td>
</tr>
<tr class="even">
<td class="dates">February</td>
<td>
<div class="cost">
<div class="EURO">€500</div>
<div class="GBPOUND" style="display:none">£350</div>
<div class="DKKRONER" style="display:none">DKK 1500</div>
</div>
</td>
</tr>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.