我正在嘗試添加到購物車按鈕,我沒有任何錯誤,但是當我按下它時按鈕沒有顯示任何內容,因為我正在使用警報功能對其進行測驗,并且數量的增量和減量按鈕也不起作用所以我認為整個 jquery 代碼有問題,但我不知道它是什么
<div class="row mt-2">
<div class="col-md-3">
<input type="hidden" value="{{$products->id}}" class="prod_id">
<label for="Quantity"> Quantity</label>
<div class="input-group text-center mb-3 " style="width: 110px">
<button class="input-group-text decrement-btn">-</button>
<input type="text" name="quantity " class="form-control qty-input text-center" value="1"/>
<button class="input-group-text increment-btn"> </button>
</div>
</div>
<div class="col-md-9">
<br>
<button type="button" class="btn btn-success me-3 float start"> Add to wishlist</button>
<button type="button" class="btn btn-success me-3 addtoCartbtn float-start"> Add to cart</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<hr>
<h3>Description</h3>
<p class="mt-3">
{!! $products->desc!!}
</p>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script>
$(document).ready(function {
$('.addtoCartbtn').click(function (e) {
e.preventDefault();
var product_id= $(this).closest('.product_data').find('.prod_id').val();
var product_qty= $(this).closest('.product_data').find('.qty-input').val();
alert(product_id);
alert(product_qty);
});
$(".increment-btn").click(function (e) {
e.preventDefault();
var inc_value=$(".qty-input").val();
var value= parsint(inc_value,10);
value= isNaN(value) ? '0': value;
if(value < 10){
value ;
$(".qty-input").val(value);
}
});
$('.decrement-btn').click(function (e) {
e.preventDefault();
var dec_value= $('.qty-input').val();
var value= parsint(dec_value,10);
value= isNaN(value) ? '0': value;
if(value > 1){
value--;
$('.qty-input').val(value);
}
});
});
</script>
@endsection
uj5u.com熱心網友回復:
您可能應該查看控制臺。
@section('scripts')
<script>
$(document).ready(function() { // changes
$('.addtoCartbtn').click(function (e) {
e.preventDefault();
var product_id= $(this).closest('.product_data').find('.prod_id').val();
var product_qty= $(this).closest('.product_data').find('.qty-input').val();
alert(product_id);
alert(product_qty);
});
$(".increment-btn").click(function (e) {
e.preventDefault();
var inc_value=$(".qty-input").val();
var value= parseInt(inc_value,10);
value= isNaN(value) ? '0': value;
if(value < 10){
value ;
$(".qty-input").val(value);
}
});
$('.decrement-btn').click(function (e) {
e.preventDefault();
var dec_value= $('.qty-input').val();
var value= parseInt(dec_value,10);
value= isNaN(value) ? '0': value;
if(value > 1){
value--;
$('.qty-input').val(value);
}
});
});
</script>
@endsection
您正在為 jquery 陣列賦值
$('.qty-input').val();
這應該改為
$('.qty-input')[0].val()
修正錯別字:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427156.html
標籤:javascript php jQuery 拉拉维尔
