問題背景的數據:JQuery的 - 無法訪問多個視圖元素
我有,其具有包含3個面板的視圖的MVC應用程序。在每個面板中都有一個按鈕,單擊按鈕時需要將面板中傳遞的Model對象的數據集傳遞給名爲「AddToCart」的控制器上的方法。
問題:
這裏是視圖的代碼:
<div class="row mePadding" id="features">
<input id="productId" type="hidden" value="@Model.ElementAt(0).Id">
<input id="productImage" type="hidden" value="@Model.ElementAt(0).ProductImage">
<input id="productBrand" type="hidden" value="@Model.ElementAt(0).ProductBrand">
<div class="col-sm-4 feature">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title boldFeatures" id="productName">@(Model.ElementAt(0).ProductName)</h3>
</div>
<div class="panel-body">
<a href="@Url.Action("ProductDetail", "Product", new { id = @Model.ElementAt(0).Id })"><img src="~/Images/@(Model.ElementAt(0).ProductImage)" class="img-circle" id="featuresImages" alt="Work"></a>
<p>@(Model.ElementAt(0).ProductSummary)</p>
<p id="productPrice">@(Model.ElementAt(0).ProductPrice)</p>
<input class="btn btn-primary btn-block AddToCart" type="button" data-name="@Model.ElementAt(0).ProductName" data-price="@Model.ElementAt(0).ProductPrice" value="Add to Cart" id="addToCart" />
</div>
</div>
</div>
<input id="productId" type="hidden" value="@Model.ElementAt(1).Id">
<input id="productImage" type="hidden" value="@Model.ElementAt(1).ProductImage">
<input id="productBrand" type="hidden" value="@Model.ElementAt(1).ProductBrand">
<div class="col-sm-4 feature">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title boldFeatures" id="productName">@(Model.ElementAt(1).ProductName)</h3>
</div>
<div class="panel-body">
<a href="@Url.Action("ProductDetail", "Product", new { id = @Model.ElementAt(1).Id })"><img src="~/Images/@(Model.ElementAt(1).ProductImage)" class="img-circle" id="featuresImages" alt="Work"></a>
<p>@(Model.ElementAt(1).ProductSummary)</p>
<p id="productPrice">@(Model.ElementAt(1).ProductPrice)</p>
<input class="btn btn-primary btn-block AddToCart" type="button" data-name="@Model.ElementAt(1).ProductName" data-price="@Model.ElementAt(1).ProductPrice" value="Add to Cart" id="addToCart" />
</div>
</div>
</div>
<input id="productId" type="hidden" value="@Model.ElementAt(2).Id">
<input id="productImage" type="hidden" value="@Model.ElementAt(2).ProductImage">
<input id="productBrand" type="hidden" value="@Model.ElementAt(2).ProductBrand">
<div class="col-sm-4 feature">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title boldFeatures" id="productName">@(Model.ElementAt(2).ProductName)</h3>
</div>
<div class="panel-body">
<a href="@Url.Action("ProductDetail", "Product", new { id = @Model.ElementAt(2).Id })"><img src="~/Images/@(Model.ElementAt(2).ProductImage)" class="img-circle" id="featuresImages" alt="Work"></a>
<p>@(Model.ElementAt(2).ProductSummary)</p>
<p id="productPrice">@(Model.ElementAt(2).ProductPrice)</p>
<input class="btn btn-primary btn-block AddToCart" type="button" data-name="@Model.ElementAt(2).ProductName" data-price="@Model.ElementAt(2).ProductPrice" value="Add to Cart" id="addToCart" />
</div>
</div>
</div>
</div>
<script>
$(this).click(function (e) {
$.ajax({
url: '@Url.Action("AddToCart")',
type: 'POST',
data: {
"id": $('#productId').val(),
"name": $('#productName').text(),
"qty": "1",
"price": $('#productPrice').text(),
"brand": $('#productBrand').val(),
"image": $('#productImage').val()
}
});
});
</script>
的問題是,它物質dosent予壓在3個面板模型數據中的第一面板中設置的按鈕總是發送給控制器的AddToCart方法。
我嘗試以下,但這並沒有工作:
"id": $(this).get('#productId').val()
任何幫助,這將是巨大的。
編輯 - 2015年12月2日
立即查看通過生成面板for循環 - 目前設定爲9這就是我要多少項目顯示在視圖:
<div class="row mePadding" id="features">
@for (int i = 0; i < 9; i++)
{
<div class="col-sm-4 feature">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title boldFeatures">@(Model.ElementAt(i).ProductName)</h3>
</div>
<div class="panel-body">
<a href="@Url.Action("ProductDetail", "Product", new { id = @Model.ElementAt(i).Id })"><img src="~/Images/@(Model.ElementAt(i).ProductImage)" class="img-circle" id="featuresImages" alt="Work"></a>
<p class="summary">@(Model.ElementAt(i).ProductSummary)</p>
<p class="price">@(Model.ElementAt(i).ProductPrice)</p>
<input class="btn btn-primary btn-block AddToCart" type="button" data-id="@Model.ElementAt(0).Id" value="Add to Cart" />
</div>
</div>
</div>
}
</div>
JQuery的按鈕點擊:
我已經刪除了邏輯的ID居然張貼到控制器上的AddToCart
方法這樣我就可以檢查ID的時候按鈕被點擊。
<script>
$(".AddToCart").click(function() {
var id = $(this).data('id');
alert(id);
});
</script>
你生成無效的HTML重複的'id'屬性。 '$('#productId')。val()'將始終返回第一個元素的值爲'id =「productId」'。刪除'id'屬性(改爲使用類名),並使用選擇器獲取值,該選擇器獲取相對於單擊按鈕的元素。 – 2015-02-10 22:53:31
@StephenMuecke謝謝你的回覆。我已經刪除了'id'屬性,並添加了類名稱。在我的JQuery中,我仍然調用它,並試圖使用以下方法使用'.prodName'選擇器 - var productName = $(「。prodName」)來獲取數據。text();'這又給了我View中的所有數據,使用'.test'類名。你能否詳細說說'相對於點擊按鈕獲取元素'? – user1352057 2015-02-11 23:17:20
當您點擊其中一個帶有「添加到購物車」值的按鈕時,是否希望觸發此操作?它有點難以理解爲什麼你不只是在一個循環中生成的HTML,而不是重複它3次,也爲什麼你甚至有隱藏的輸入。 – 2015-02-11 23:25:46