gpt4 book ai didi

javascript - 单击内部按钮时防止单击父 A 标记

转载 作者:行者123 更新时间:2023-12-02 21:46:02 27 4
gpt4 key购买 nike

我在索引页上的产品卡上遇到问题。在产品卡内部,我有 Vue 组件来渲染表单(数量和添加到购物车按钮)。当我单击“添加到购物车”按钮时,我得到了预期的结果。响应被发送到根 vue 组件,然后我看到产品已添加到购物车的 Toast 通知。但是,当我单击加号、减号按钮或输入字段时,会从父 A 标记触发 href 链接。

如何禁用它?我找到了这个Prevent on click on parent when clicking button inside div

但只有当我将 A 标签放入 vue 组件中时它才有效。我不想在 vue 中放入太多的 html。

        @foreach ($products as $product)
<a href="{{ $category->fullpath.'/'.$product->sef }}" class="catalog__card">
<div class="catalog__card-img"><img src="/storage/img/products/{{ $product->mediumpic }}" alt="{{ $product->title }}"></div>

<div class="card__properties">
<div class="card__price"><span>{{ $product->price }}<span class="currencySymbol">₽</span></span></div>
<div class="card__stock">
@if ( $product->stock > 0 )
<i class="far fa-check-circle text-success"></i><span class="text-success"><strong> on stock</strong></span>
@else
<span><strong> on request</strong></span>
@endif
</div>
<addtocart @added_to_cart="updateCart"></addtocart>
</div>
<div class="catalog__card-title"><span>{{ $product->title }}</span></div>
</a>
@endforeach

在 Vue 组件中我有以下内容

<template>

<div class="cart">
<form method="POST" id="add_to_cart" action="/add_to_cart" @submit.prevent="onSubmit">
<input type="hidden" name="_token" :value="csrf">
<div class="quantity">
<button type="button" class="minus_quantity" v-on:click="minus_quantity" v-long-press="300" @long-press-start="longMinusStart" @long-press-stop="longMinusStop">-</button>
<input type="number" class="input-text qty text" step="1" min="1" max="9999" name="quantity" value="1" title="Qty" v-model.number="quantity">
<button type="button" class="plus_quantity" v-on:click="plus_quantity" v-long-press="300" @long-press-start="longPlusStart" @long-press-stop="longPlusStop">+</button>
</div>
<button type="submit" name="add-to-cart" class="button-cart"><i class="fas fa-cart-plus"></i><span> order</span></button>
</form>
</div>

</template>


<script>
import LongPress from 'vue-directive-long-press';

export default {

name: "addtocart",

data: function () {
return {
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
quantity: 1,
plusInterval: null,
minusInterval: null,
success: false,
errors: []
}
},

directives: {
'long-press': LongPress,
},


props: [],


computed: {
getName(){
return this.$store.getters.Name
}
},

methods: {
// add to cart quantity plus and minus buttons
// short mouse click event
parent() { alert('you clicked the parent') },

minus_quantity() {
if (this.quantity > 0) {this.quantity -= 1}
},
plus_quantity() {this.quantity += 1},
// long press buttons
longPlusStart() {
this.plusInterval = setInterval(() => {
this.quantity += 1
}, 100)
},
longPlusStop() {
clearInterval(this.plusInterval)
},
longMinusStart() {
this.minusInterval = setInterval(() => {
if (this.quantity > 0) {this.quantity -= 1}
}, 100)
},
longMinusStop() {
clearInterval(this.minusInterval)
},

onSubmit() {
axios.post('/add_to_cart', this.$data)
.then(this.onSuccess)
.catch(error => this.errors = error.response.data);
},
onSuccess(response) {
this.success = true;
this.$emit('added_to_cart', response);
},

},

mounted() {


},


}
</script>

最佳答案

您可以对加号、减号按钮使用“v-on:click.stop”指令,而不是“v-on:click”

阅读此内容以获取更多信息 https://v2.vuejs.org/v2/guide/events.html

关于javascript - 单击内部按钮时防止单击父 A 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60248591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com