私は異なる属性を持つ車の配列を持っています。私はボタンを押すとミニバンだけを表示する機能を作りようとしています。ボタンを押したときに何も表示されません。私は完全なプログラムを実行するとエラーは発生しません。私が間違っていることについてのヒントは非常に役に立ちます!配列から特定のプロパティを表示
// Define car object using a constructor function
function Car(id, car_make, car_model, car_year, car_type, car_color,
car_price, car_mileage) {
this.stockid = id;
this.make = car_make;
this.model = car_model;
this.year = car_year;
this.type = car_type;
this.color = car_color;
this.price = car_price;
this.mileage = car_mileage;
}
// Declare an array of Car objects
var car_list = [];
// Create an instance of the Car object and add it to the car_list array
car_list.push(new Car(1001, "Toyota", "Camry", 2011, "Sedan", "Gray",
17555, 55060));
car_list.push(new Car(1002, "Volvo", "s40", 2013, "Sedan", "Black",
15575, 20350));
car_list.push(new Car(1251, "Toyota", "Sienna", 2008, "Minivan", "Gray",
15775, 70000));
car_list.push(new Car(1321, "Porsche", "Panamera", 2012, "Hatchback",
"Red", 104250, 10567));
car_list.push(new Car(1904, "Honda", "Accord", 2009, "Sedan", "White",
13370, 35000));
car_list.push(new Car(1855, "Toyota", "Highlander", 2008, "SUV",
"Silver", 18555, 55060));
car_list.push(new Car(1543, "Ford", "Fusion", 2011, "Sedan", "Black",
13575, 90350));
car_list.push(new Car(1345, "Toyota", "Sienna", 2011, "Minivan", "Gray",
25775, 70000));
car_list.push(new Car(2133, "Dodge", "Caravan", 2012, "Minivan", "Red",
30250, 17567));
car_list.push(new Car(2999, "Lexus", "LFA", 2012, "coupe", "Red", 381370,
3500));
car_list.push(new Car(3001, "Ferrari", "Rubino", 2012, "coupe", "Red",
354370, 5500));
car_list.push(new Car(4002, "Audi", "R8", 2012, "convertible", "Black",
181370, 4500));
// Display car list
for (var i=0; i<car_list.length; i++){
var this_element = "<tr class='car-item' id='l-"+i+"' >" ;
this_element += "<td>" + car_list[i].stockid + "</td><td>" +
car_list[i].make + "</td>";
this_element += "<td>" + car_list[i].model + "</td><td>" +
car_list[i].year + "</td><td>" + car_list[i].type + "</td>";
this_element += "<td>" + car_list[i].color + "</td><td> $" +
car_list[i].price + "</td>";
this_element += "<td>" + car_list[i].mileage + "</td>";
this_element += "<td><button type='button' data-index='"+i+"'
class='addme btn btn-primary' ";
this_element += "data-stockid='"+ car_list[i].stockid + "'>Add</button>
</td></tr>";
$("#car-list").append(this_element);
}
//display minivans
$('#p3').on('click', function(){
if(type = "Minivan"){
var thisButton = $(this);
var index = thisButton.data('index');
var thisCar = car_list[index];
var newRow = "<tr><td>"+ thisCar.stockid + "</td><td>" + thisCar.make + "
</td><td>" + thisCar.model + "</td><td>" + thisCar.year +
"</td><td>" + thisCar.type + "</td><td>" + thisCar.color + "</td><td>" +
thisCar.price + "</td><td>" + thisCar.mileage + "</td></tr>";
$('#minivan-list').append(newRow);
}
});
にすべてのミニバンを追加したい場合はあなたのクリックハンドラであなたの車のリストをループしていません。 –
'type'がどこにも定義されていないので、ブラウザコンソールでエラーが表示されます – charlietfl
HTMLコードも入力できますか? – klugjo