2017-05-08 149 views
0

我搜索瞭如何根據下拉列表中的選項顯示/隱藏列。我寫了這個:根據下拉列表中的選項顯示/隱藏列

function categoriesCriteres() { 
 
    var sections = document.getElementById("sections").value; 
 
    if (sections == "generaux") { 
 
    document.getElementByClassName("generaux").style.display = "block"; 
 
    } else { 
 
    document.getElementByClassName("generaux").style.display = "none"; 
 
    } 
 
}
<select class="form-control" id="sections" name="sections" onchange="categoriesCriteres()"> 
 
    <option value="choisir" selected disabled>Choisir</option> 
 
    <option id="generaux" value="generaux">Apports généraux</option> 
 
    <option id="mineraux" value="mineraux">Minéraux</option> 
 
    <option id="vitamines" value="vitamines">Vitamines</option> 
 
    <option id="autres" value="autres">Autres</option> 
 
    </select> 
 
<div> 
 
    <table class="table table-striped" id="nutrition"> 
 
    <thead> 
 
     <tr> 
 
     <th>Aliments</th> 
 
     <th>Poids</th> 
 
     <th class="generaux">Energie kJ</th> 
 
     <th class="generaux">Energie kcal</th> 
 
    </thead> 
 
    <tbody class="text-primary" id="myrecap"> 
 
     <tr> 
 
     <td>blé</td> 
 
     <td><strong>150gr</strong></td> 
 
     <td class="generaux">energie_kJ</td> 
 
     <td class="generaux">energie_kcal</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Total</td> 
 
     <td><strong>150 gr</strong></td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

但是,當我在我的下拉列表中更改數值沒有發生......我不明白什麼是錯的....也許有人可以幫我嗎?

+0

其實,事情確實發生了:你得到一個錯誤。我建議你看看那個。錯誤是沒有'document.getElementByClassName()'這樣的函數。 –

+0

http://stackoverflow.com/questions/4398966/how-can-i-hide-select-options-with-javascript-cross-browser –

+0

沒有'document.getElementByClassName()'它的'document.getElementsByClassName() '筆記'' – Khaleel

回答

0

使用jQuery的。它是一個JavaScript Library.It大大簡化了JavaScript編程。

代碼

var sections = document.getElementById("sections").value; 

這將返回選擇下拉選項值的工作。 然後,您需要比較所選選項是否爲generaux

如果爲true,我們將所有類別的元素的display屬性設置爲table-cell。

如果爲假,即;用戶選擇了其他選項。然後我們將所有generaux元素的display屬性設置爲none。

$('.generaux')document.getElementsByClassName('generaux')相似。

$('.generaux').each(function() { 
      this.style.display = "table-cell" 
}); 

作品作爲

var generauxElements = document.getElementsByClassName("generaux"); 
for(var i=0,length=generauxElements.length; i<length; i++){ 
    generauxElements[i].style.display="table-cell"; 
} 

​​3210的方法的替代方案中被設計爲使DOM循環結構簡潔和不易出錯。當它被調用時,它遍歷作爲jQuery對象一部分的DOM元素。每次回調運行時,都會從當前循環迭代開始,從0開始。更重要的是,回調在當前DOM元素的上下文中觸發,因此關鍵字this指的是元素。

if (sections == "generaux") { 
    $('.generaux').each(function() { 
     this.style.display = "table-cell" 
    }); 
    } else { 
    $('.generaux').each(function() { 
     this.style.display = "none" 
    }); 
    } 

使用display: block;粉碎表格對齊。所以最好使用display: table-cell;

function categoriesCriteres() { 
 
    var sections = document.getElementById("sections").value; 
 
    if (sections == "generaux") { 
 
    $('.generaux').each(function() { 
 
     this.style.display = "table-cell" 
 
    }); 
 
    } else { 
 
    $('.generaux').each(function() { 
 
     this.style.display = "none" 
 
    }); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select class="form-control" id="sections" name="sections" onchange="categoriesCriteres()"> 
 
      <option value="choisir" selected disabled>Choisir</option> 
 
      <option id="generaux" value="generaux">Apports généraux</option> 
 
      <option id="mineraux" value="mineraux">Minéraux</option> 
 
      <option id="vitamines" value="vitamines">Vitamines</option> 
 
      <option id="autres" value="autres">Autres</option> 
 
     </select> 
 

 
<div> 
 
    <table class="table table-striped" id="nutrition"> 
 
    <thead> 
 
     <tr> 
 
     <th>Aliments</th> 
 
     <th>Poids</th> 
 
     <th class="generaux">Energie kJ</th> 
 
     <th class="generaux">Energie kcal</th> 
 
    </thead> 
 
    <tbody class="text-primary" id="myrecap"> 
 

 
     <tr> 
 
     <td>blé</td> 
 
     <td><strong>150gr</strong></td> 
 
     <td class="generaux">energie_kJ</td> 
 
     <td class="generaux">energie_kcal</td> 
 
     </tr> 
 

 
     <tr> 
 
     <td>Total</td> 
 
     <td><strong>150 gr</strong></td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

你應該補充說,你使用jQuery,我們不知道他是否不使用它。儘管如此,它應該工作;) – Diego

+0

非常感謝@Geethu Jose它的工作原理!您正在使用Jquery它對我來說是全新的...所以現在我必須使用這個代碼我的下拉列表中的每個選項我想? – ginette

+0

jQuery是一個JavaScript庫。它極大地簡化了JavaScript編程。 –

0

它是getElementsByClassName()而不是getElementByClassName()它返回和數組。您必須使用[]括號表示法訪問陣列的每個元素。

function categoriesCriteres() { 
 
var sections = document.getElementById("sections").value; 
 
if (sections == "generaux") { 
 
var g = document.getElementsByClassName("generaux"); 
 
for(var i=0; i<g.length; i++){ 
 
    g[i].style.display="block"; 
 
} 
 
} else { 
 
var g = document.getElementsByClassName("generaux"); 
 
for(var i=0; i<g.length; i++){ 
 
    g[i].style.display="none"; 
 
} 
 
} 
 
}
<select class="form-control" id="sections" name="sections" onchange="categoriesCriteres()"> 
 
     <option value="choisir" selected disabled>Choisir</option> 
 
     <option id="generaux" value="generaux">Apports généraux</option> 
 
     <option id="mineraux" value="mineraux">Minéraux</option> 
 
     <option id="vitamines" value="vitamines">Vitamines</option> 
 
     <option id="autres" value="autres">Autres</option> 
 
    </select> 
 

 
<div> 
 
<table class="table table-striped" id="nutrition"> 
 
    <thead> 
 
    <tr> 
 
    <th>Aliments</th> 
 
    <th>Poids</th> 
 
    <th class="generaux">Energie kJ</th> 
 
    <th class="generaux">Energie kcal</th> 
 
     </thead> 
 
    <tbody class="text-primary" id="myrecap"> 
 

 
     <tr> 
 
    <td>blé</td><td><strong>150gr</strong></td> 
 
     <td class="generaux">energie_kJ</td> 
 
     <td class="generaux">energie_kcal</td> 
 
    </tr> 
 

 
    <tr><td>Total</td><td><strong>150 gr</strong></td></tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

非常感謝@ VaTz88它的工作原理! – ginette

+0

我不是使用jQuery,它純粹是JavaScript – vatz88

+0

是的,我很抱歉,我以爲我回答了下面的帖子...與jquery ... – ginette

相關問題