admin管理员组文章数量:1389762
So I have tried to display the content of a table based on the country of a user. basically if a user selects the united states as option, he gets a table row containing US data displayed.. I used a select with options, and every option has the domain extension of country as a value ( for example, for Brazil option, the value is br : <option value="br">Brazil</option>
, plus the table row for it has the same option value as an ID ( <tr id="br" style="display: none;">..</tr>
)
Is there any script to display a country data once the user selects that country in the options ?
Thank you, and here's my code:
HTML:
<select id="data">
<option value="#">Select a country ..</option>
<option value="us">United States</option>
<option value="es">Spain</option>
<!-- more countries -->
</select>
<div id="container">
<table border="1">
<tr>
<td> Country </td>
<td> Chance</td>
</tr>
<tr id="us" style="display: none;">
<td> United States </td>
<td> 2.02 % </td>
</tr>
<tr id="es" style="display: none;">
<td> Spain </td>
<td> 2.12 % </td>
</tr>
</table>
</div>
I tried the following:
Jquery:
$('#data').change(function() {
$('tr#($(this).val())').css('display','block');
});
Regards,
Thank you!
So I have tried to display the content of a table based on the country of a user. basically if a user selects the united states as option, he gets a table row containing US data displayed.. I used a select with options, and every option has the domain extension of country as a value ( for example, for Brazil option, the value is br : <option value="br">Brazil</option>
, plus the table row for it has the same option value as an ID ( <tr id="br" style="display: none;">..</tr>
)
Is there any script to display a country data once the user selects that country in the options ?
Thank you, and here's my code:
HTML:
<select id="data">
<option value="#">Select a country ..</option>
<option value="us">United States</option>
<option value="es">Spain</option>
<!-- more countries -->
</select>
<div id="container">
<table border="1">
<tr>
<td> Country </td>
<td> Chance</td>
</tr>
<tr id="us" style="display: none;">
<td> United States </td>
<td> 2.02 % </td>
</tr>
<tr id="es" style="display: none;">
<td> Spain </td>
<td> 2.12 % </td>
</tr>
</table>
</div>
I tried the following:
Jquery:
$('#data').change(function() {
$('tr#($(this).val())').css('display','block');
});
Regards,
Thank you!
Share Improve this question asked Apr 13, 2015 at 19:05 IsmailIsmail 471 silver badge5 bronze badges2 Answers
Reset to default 2Thats not the way to concatenate, try this instead:
$('#data').change(function() {
$('tr#' + $(this).val()).css('display','block');
});
EDIT with the OP ment:
Add a class to all the TRs that should hide/show, and then:
<table border="1">
<tr>
<td> Country </td>
<td> Chance</td>
</tr>
<tr class="hideShowTr" id="us" style="display: none;">
<td> United States </td>
<td> 2.02 % </td>
</tr>
<tr class="hideShowTr" id="es" style="display: none;">
<td> Spain </td>
<td> 2.12 % </td>
</tr>
</table>
$('#data').change(function() {
$('.hideShowTr').css('display','none');
$('tr#' + $(this).val()).css('display','block');
});
Try this:
$('#data').change(function() {
//Only show specific row
$('tr#' + $(this).val()).show();
//hide other rows
$("tr:not(:#"+$(this).val()+")").hide();
});
本文标签: javascriptDisplay table row based on the selected option of Select html tagStack Overflow
版权声明:本文标题:javascript - Display table row based on the selected option of Select html tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744722984a2621806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论