Peter Hansen Peter Hansen - 6 months ago 10
Javascript Question

click on button dont trigger jquery function

I have a Partial View in my MVC webshop that displays my shopping cart, but for some reason I'm not able to click the "trash" button.

This is my current code.



@model ShoppingCart
@foreach (var product in Model.Items)
{
<li>
<div class="b-cart-table ">
<a href="#" class="image">
<img width="70" height="70" src="@product.ImageUrl" alt="/">
</a>
<div class="caption">
<a class="product-name" href="#">@product.Name</a>
<span class="product-price">@product.Quantity x $ @product.Price.ToString("00.00").Replace(",", ".")</span>
<div class="rating">
<span class="star"><i class="fa fa-star"></i></span>
<span class="star"><i class="fa fa-star"></i></span>
<span class="star"><i class="fa fa-star"></i></span>
<span class="star"><i class="fa fa-star"></i></span>
<span class="star star-empty"><i class="fa fa-star-o"></i></span>
</div>
</div>

<button onclick="remove();" class="btn btn-remove removeitem"><i class="fa fa-trash fa-lg"></i></button>
</div>
</li>
}
<li>
<div class="products-subtotal text-right">
Cart Subtotal <span class="subtotal-price">$ @Model.TotalPrice().ToString("00.00").Replace(",", ".")</span>
</div>
</li>

@section scripts{
<script>
$(document).ready(function () {
$(document).on("click", ".removeitem", function () {
alert($(this).text());
});

var remove = function(){
alert("trigger");
}
};
</script>
});





I know that there is multiple click / function on the button but its just to show what I have tried so far.

Answer

I'm kinda sure its your:

@section scripts{
}

I can't explain 100% why but when I use this inside a partial View I cant make my code work either. Try remove your code to your layout page or the page that contains the partialview and try then.

Comments