我試圖通過JavaScript擺脫嵌套數組數據並顯示JavaScript的嵌套數組提取
我已創建這樣
var products = {Elecrtonics:["Mobile","Tablet","Laptop"],
fasion:["celio","UCB","PE"],sports:["Puma","Rebock","Nike"]
};
數組我有一個名爲Electronics, Fasion, Sports
三個按鈕,當我在電子點擊按鈕它應該獲取並顯示電子陣列["Mobile","Tablet","Laptop"]
相同的時間和體育
我應該怎麼做呢?或者我會錯在哪裏?
我的HTML和JS的全是這樣的
<div class = "container" id="formcontainer">
<form>
<div class="form-group col-lg-4 col-md-offset-3">
<h1>Welcome</h1>
<input type="text" class="form-control search" id="search" placeholder="What is in your mind?">
</div>
<div class="form-group col-lg-4 col-md-offset-3">
<button class="btn btn-primary" onClick="getData(Electronics)">Elecrtonics</button>
<button class="btn btn-primary" onClick="getData(Fasion)">Fasion</button>
<button class="btn btn-primary" onClick="getData(Sports)">Sports</button>
</div>
<p id = "myProducts"></p>
</form>
</div>
<script>
var products = {Elecrtonics:["Mobile","Tablet","Laptop"],fasion:["celio","UCB","PE"],sports:["Puma","Rebock","Nike"]};
function getData (el){
document.getElementById("myProducts").innerHTML = products.el;
event.preventDefault()
}
</script>
'產品[EL]',你需要傳遞參數的引號即'getSports('Sports')' – Satpal
檢查[link](https://jsfiddle.net/4o3s2j3L/) – CommonPlane
我想知道我的方法是否正確,因爲我無法獲得所需的輸出。我需要一些建議 –