我目前正在某人的网站上工作,我为每种产品创建了6种不同的模态。但是当我点击模态按钮时,我想在每个模态中插入他的内容。
到目前为止,我已经这样做了:
function produits(tag, nom, price, imagesrc, description){
this.Tag = tag;
this.Nom = nom;
this.Price = price;
this.SRC = imagesrc;
this.Description = description;
}
var produit = [];
//6x for my 6 products
produit.push(new produits(1, "nom", 35, "path/to/img","Description"));
In html I created my modals 6x this way
<button type="button" id="bouton" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Add to cart</button>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
现在我想将我存储在数组中的6种产品的内容添加到我的模态中。请有人帮我这个吗?
非常感谢你。
约翰
答案 0 :(得分:0)
看看这个小提琴modal dynamic content using javascript。
在此我已将产品索引作为1.您可以通过为另外5个产品创建数组并将索引分别传递为2,3,4,5和6来测试它。
请注意,我在点击按钮时调用了showProduct()函数。
HTML:
<button type="button" id="bouton" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" onclick="showProduct(1);">Add to cart</button>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
<div class="row">
<span><b>Product tag : </b></span><span id="product_tag"></span>
</div>
<div class="row">
<span><b>Product nom : </b></span>
<span id="product_nom"></span>
</div>
<div class="row">
<span><b>Product price : </b></span>
<span id="product_price"></span>
</div>
<div class="row">
<span><b>Product image : </b></span>
<img id="product_image" src=""/>
</div>
<div class="row">
<span><b>Product description : </b></span>
<span id="product_description"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Javascript: -
function produits(tag, nom, price, imagesrc, description){
this.Tag = tag;
this.Nom = nom;
this.Price = price;
this.SRC = imagesrc;
this.Description = description;
}
var produit = [];
//6x for my 6 products
produit.push(new produits(1, "nom", 35, "path/to/img","Description"));
function showProduct(productId){
document.getElementById("product_tag").innerHTML = produit[productId-1].Tag;
document.getElementById("product_nom").innerHTML = produit[productId-1].Nom;
document.getElementById("product_price").innerHTML = produit[productId-1].Price;
document.getElementById("product_image").src = produit[productId-1].SRC;
document.getElementById("product_description").innerHTML = produit[productId-1].Description;
}