Javascript 选择下拉列表时,必须显示不同的表格
我为10个不同的批次设置了10个表,并为用户设置了一个下拉列表,以选择他们想要查看的批次列表。选择该选项后,将显示相应的批次表 所有其他表都应该隐藏,但在加载页面时。请为我提供至少3个表的示例代码 提前感谢。使用下面的代码 JavaScript:Javascript 选择下拉列表时,必须显示不同的表格,javascript,php,jquery,Javascript,Php,Jquery,我为10个不同的批次设置了10个表,并为用户设置了一个下拉列表,以选择他们想要查看的批次列表。选择该选项后,将显示相应的批次表 所有其他表都应该隐藏,但在加载页面时。请为我提供至少3个表的示例代码 提前感谢。使用下面的代码 JavaScript: <script type="text/javascript"> function showForm() { var selopt = document.getElementById("ID").value; if
<script type="text/javascript">
function showForm() {
var selopt = document.getElementById("ID").value;
if (selopt ==1) {
document.getElementByID("f1").style.display = "block";
document.getElementByID("f2").style.display = "none";
document.getElementByID("f3").style.display = "none";
}
if (selopt==2){
document.getElementByID("f1").style.display = "none";
document.getElementByID("f2").style.display = "block";
document.getElementByID("f3").style.display = "none";
if (selopt==3){
document.getElementByID("f1").style.display = "none";
document.getElementByID("f2").style.display = "none";
document.getElementByID("f3").style.display = "block";
}
</script>
函数showForm(){
var selopt=document.getElementById(“ID”).value;
如果(selopt==1){
document.getElementByID(“f1”).style.display=“block”;
document.getElementByID(“f2”).style.display=“无”;
document.getElementByID(“f3”).style.display=“无”;
}
如果(selopt==2){
document.getElementByID(“f1”).style.display=“无”;
document.getElementByID(“f2”).style.display=“block”;
document.getElementByID(“f3”).style.display=“无”;
如果(selopt==3){
document.getElementByID(“f1”).style.display=“无”;
document.getElementByID(“f2”).style.display=“无”;
document.getElementByID(“f3”).style.display=“block”;
}
HTML将如下所示:
<form action = "sample.com" method= "post">
<select id="ID" onchange = "showForm()">
First drop down
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
<div id = "f1" style = "display:none">
second table
</div>
<div id = "f2" style = "display:none">
third table
</div>
<div id = "f3" style = "display:none">
fourth table
</div>
</form>
第一个下拉列表
第二桌
第三桌
第四桌
使用函数
例如:
$( ".target" ).toggle();
您需要为下拉列表添加更改处理程序。更改时,您将切换与所选元素链接的表
您的html将是:
<table id='table1'>....</table>
<table id='table3'>....</table>
<table id='table3'>....</table>
//.... etc
<select id='dropdown'>
<option value='table1'>Show table 1</option>
<option value='table2'>Show table 2</option>
<option value='table3'>Show table 3</option>
//....
</select>
注意,您必须在css中为每个表添加display:none;
属性,这样它们就不会在页面加载时显示,并且toggle()
函数将正常工作
CSS:
HTML:
<select>
<option value="-1"> Select</option>
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
<table id="one" class="table">
<tr>
<td>
one
</td>
</tr>
</table>
<table id="two" class="table">
<tr>
<td>
two
</td>
</tr>
</table>
<table id="three" class="table">
<tr>
<td>
three
</td>
</tr>
</table>
$('select').change(function(){
if($(this).val() != "-1")
{
$('table.table').hide();
$('table#'+$(this).val()).show();
}
})
为什么是3张桌子?为什么不是4张桌子?4张桌子也是受欢迎的…但我这么说是为了方便你的……PSo:我们为什么要这样做?你在哪里被卡住了?你有没有尝试过,但没有成功,出了什么问题?这看起来像是你在给我们布置任务,或者期待我们做(你的)代表您工作。我们会提供帮助,但前提是您已经进行了实际的尝试,并且能够演示您的非工作尝试。或者甚至是没有完全成功的工作尝试;请展示一些努力。请。@user3613379任何回答对您有帮助吗?嗨,朋友们,我不知道为什么,当我编写代码时,之前我创建了一个工作正常的代码,但我我不知道我在哪里犯了错误…没有一个在工作…很抱歉,但我对jquery没有什么好主意…(它不工作。我的代码看起来是这样的。我在哪里犯了错误请指导我。table{display:none;}google.load(“jquery”,“1.3.2”);$(“#dropdown”)。on('change',function(){$((“table”)。每个(函数)(){//hide all tables$(this.hide();});var tableId=$(“:selected”,this.val();//获取链接表id$(“table#“+tableId+”).toggle();//切换链接表});首先,用你的fide更新你的问题。你看了演示吗?它是有效的代码。演示是有效的。即使在android Firefox中,Fiddle示例也是有效的browser@user3613379+您的代码中有一个错误:
结尾处是的,它起作用意味着您的代码..我也尝试了一个,而且也起作用。我正在做的就是用它编写代码记事本..我需要localhost之类的服务器吗?因为我只是在记事本中运行代码,html部分工作正常,但jquery代码不工作..:(
<select>
<option value="-1"> Select</option>
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
<table id="one" class="table">
<tr>
<td>
one
</td>
</tr>
</table>
<table id="two" class="table">
<tr>
<td>
two
</td>
</tr>
</table>
<table id="three" class="table">
<tr>
<td>
three
</td>
</tr>
</table>
$('select').change(function(){
if($(this).val() != "-1")
{
$('table.table').hide();
$('table#'+$(this).val()).show();
}
})