2014-07-07 114 views
1

我試圖根據所選下拉列表的值顯示不同的表單。我已經掌握了幾乎所有的工作,雖然它似乎是兩個值都顯示相同的形式,它給了我一個錯誤。Javascript:試圖根據下拉值顯示不同的表單?

這裏是the jsfiddle

,代碼:

HTML:

<select id="C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown" name="form_select"> 
    <option value="">-- Select an Option --</option> 
    <option value="Buy A Product">Buy A Product</option> 
    <option value="Support">Support</option> 
    <option value="Ask A Question">Ask A Question</option> 
    <option value="Literature Request">Literature Request</option> 
    <option value="AE Account">A&E Account</option> 
</select> 
<div id="C006_ctl00_ctl00_C006">Zip Code</div> 
<div id="C007_ctl00_ctl00_C001">Support</div> 
<div id="C008_ctl00_ctl00_C003">Ask A Question</div> 
<div id="C009_ctl00_ctl00_formControls">Brochures</div> 
<div id="C012_ctl00_ctl00_formControls">A&E Account Header</div> 
<div id="C013_ctl00_ctl00_formControls">Literature Header</div> 
<div id="C010_ctl00_ctl00_formControls">Street Address</div> 
<div id="C006_ctl00_ctl00_C008">Product Interest</div> 

CSS:

#C006_ctl00_ctl00_C006{display:none;} 
#C006_ctl00_ctl00_C008{display:none;} 
#C007_ctl00_ctl00_C001{display:none;} 
#C008_ctl00_ctl00_C003{display:none;} 
#C009_ctl00_ctl00_formControls{display:none;} 
#C010_ctl00_ctl00_formControls{display:none;} 
#C013_ctl00_ctl00_formControls{display:none;} 
#C012_ctl00_ctl00_formControls{display:none;} 

的Javascript:

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var style = this.value == "Buy A Product" ? 'block' : 'none'; 
    document.getElementById('C006_ctl00_ctl00_C006').style.display = style; 
    document.getElementById('C006_ctl00_ctl00_C008').style.display = style; 
}); 

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var style = this.value == "Support" ? 'block' : 'none'; 
    document.getElementById('C007_ctl00_ctl00_C001').style.display = style; 
}); 

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var style = this.value == "Ask A Question" ? 'block' : 'none'; 
    document.getElementById('C008_ctl00_ctl00_C003').style.display = style; 
}); 

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var style = this.value == "Literature Request" ? 'block' : 'none'; 
    document.getElementById('C010_ctl00_ctl00_formControls').style.display = style; 
    document.getElementById('C009_ctl00_ctl00_formControls').style.display = style; 
}); 

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var style = this.value == "AE Account" ? 'block' : 'none'; 
    document.getElementById('C012_ctl00_ctl00_formControls').style.display = style; 
    document.getElementById('C010_ctl00_ctl00_formControls').style.display = style; 
}); 

所以當選擇'文獻請求'時,它應該顯示小冊子和街道地址。當選擇A & E賬戶時,它應該顯示'A & E賬戶頭'和'街道地址'。兩者都應該顯示「街道地址」,儘管只有A帳戶顯示街道地址。有誰知道爲什麼?我在JavaScript上並不是那麼棒......我爲可怕的ID名稱道歉。他們自動得到,我沒有改變他們。

回答

1

使用switch語句。我沒有做這件事,但它應該讓你開始

fiddle

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    document.getElementById('C006_ctl00_ctl00_C006').style.display = 'none'; 
    document.getElementById('C006_ctl00_ctl00_C008').style.display = 'none'; 
    document.getElementById('C008_ctl00_ctl00_C003').style.display = 'none'; 
    document.getElementById('C010_ctl00_ctl00_formControls').style.display = 'none'; 
    document.getElementById('C009_ctl00_ctl00_formControls').style.display = 'none'; 
    document.getElementById('C012_ctl00_ctl00_formControls').style.display = 'none'; 
    document.getElementById('C013_ctl00_ctl00_formControls').style.display = 'none'; 
    switch (this.value) { 
     case "Buy A Product": 
       // show "Buy A Product" fields here etc 
      break; 
     case "Support": 
      break; 
     case "Ask A Question": 
      break; 
     case "Literature Request": 
      break; 
     case "AE Account": 
      document.getElementById('C010_ctl00_ctl00_formControls').style.display = 'block'; 
      document.getElementById('C012_ctl00_ctl00_formControls').style.display = 'block'; 
      break; 
    } 
}); 
+0

除了某些原因,「支持」的ID以外幾乎完全可行。在我選擇顯示「支持」後,支持繼續顯示其餘的下拉選擇。但這只是爲了這個div,這很奇怪。雖然我可能會弄清楚..謝謝! – user3802315

+0

這是最奇怪的事情,任何與'支持'的ID都不起作用。我不確定它是否只對我這麼做:http://jsfiddle.net/miiicheellee/Dzd2w/ 如果我使用不同的ID而不是'support',那麼它就可以工作。否則'支持'是粘性的。很奇怪。 – user3802315

+0

這結束了工作完美 - 謝謝你! – user3802315

2

首先,一些風格。有沒有必要連接所有這些事件偵聽器,因爲剛上可以做的伎倆:

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown') 
     .addEventListener('change', function() { 
    var value = this.value; 
    if (value === "Buy A Product") { 
     ... 
    } else if (value === "Support") { 
     ... 
    } ... 
}); 

每茨艾倫的一個分支if內,跟蹤你要顯示的元素。你可以節省一些打字保存ID,而不是:

if (value === "Buy A Product") { 
    var show = ['C006_ctl00_ctl00_C006', 'C006_ctl00_ctl00_C008']; 
} else if (...) {... 

在的,如果分支結束,隱藏的每一個元素,除了那些show數組中:

[ 
    "C006_ctl00_ctl00_C006", 
    "C007_ctl00_ctl00_C001", 
    "C008_ctl00_ctl00_C003", 
    "C009_ctl00_ctl00_formControls", 
    "C012_ctl00_ctl00_formControls", 
    "C013_ctl00_ctl00_formControls", 
    "C010_ctl00_ctl00_formControls", 
    "C006_ctl00_ctl00_C008" 
].forEach(function(id) { 
    var style = show.indexOf(id) > -1 ? "block" : "none"; 
    document.getElementById(id).style.display = style; 
}); 

這應該做的伎倆。我想你有一些問題,隨時提問。

您的問題取決於您使用多個事件偵聽器的事實,當您更改select元素的值時,它們會運行全部。所以最後一個(大概)在最後一個執行時「勝過」其他的,並相應地隱藏/顯示元素。

+0

呃,我無能。我瞭解if/else和你展示每個div的方式,但我無法弄清forEach。 – user3802315

+0

['for forach'](https://開發者。mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)是Javascript數組的一種方法。你可以在上面的鏈接中找到一些例子。 – MaxArt

0

這樣做的原因行爲是,當你選擇該選項,在事件發生後的事件處理程序AE Account運行Literature Request的處理程序。我建議只使用一個事件處理程序。沿着這些線的東西是更多的可讀性:

var elts = { 
    address: document.getElementById('C010_ctl00_ctl00_formControls'), 
    brochures: document.getElementById('C009_ctl00_ctl00_formControls'), 
    accHeader: document.getElementById('C012_ctl00_ctl00_formControls'), 
    litHeader: document.getElementById('C010_ctl00_ctl00_formControls'), 
    zip: document.getElementById('C006_ctl00_ctl00_C006'), 
    interest: document.getElementById('C006_ctl00_ctl00_C008'), 
    question: document.getElementById('C008_ctl00_ctl00_C003'), 
    support: document.getElementById('C007_ctl00_ctl00_C001') 
} 

document.getElementById('C002_ctl00_ctl00_C022_ctl00_ctl00_dropDown').addEventListener('change', function() { 
    var toShow = { 
     address: 0, brochures: 0, accHeader: 0, litHeader: 0, zip: 0, interest: 0, question: 0, support: 0 
    }; 
    switch(this.value == "Buy A Product") { 
    case "Buy A Product": toShow.interest = 1; toShow.zip = 1; break; 
    case "Support": toShow.support = 1; break; 
    case "Ask A Question": toShow.question = 1; break; 
    case "Literature Request": toShow.brochures = 1; toShow.address = 1; break; 
    case "AE Account": toShow.accHeader = 1; toShow.address = 1; break; 
    } 

    for(var i in toShow) { 
     elts[i].style.display = toShow[i] ? 'block' : 'none'; 
    } 
});