【发布时间】:2019-03-20 18:48:56
【问题描述】:
单击按钮时,我需要获取商品价格。这是我目前的代码:
<div id="shop" class="row" v-cloak>
{{#each items}}
<img src="{{img}}">
<p>{{title}}</p>
<p class="price">{{price}}</p><br>
<button class="btn btn-danger" @click="buyItem">Buy</button>
{{/each}}
</div>
JS:
methods: {
buyItem: function(event){
console.log($( event.target ).closest( ".price" ).text());
}
}
这会在 console.log 中返回空白行。如何获得 price 值?
编辑
server.js(从这里获取项目):
res.render('game/index', {
items
});
使用小胡子来展示物品:
index.hbs:
<div id="shop" class="row" v-cloak>
{{#each items}}
<img src="{{img}}">
<p>{{title}}</p>
<p class="price">{{item.price}}</p><br>
<button class="btn btn-danger" @click="buyItem">Buy</button>
{{/each}}
</div>
【问题讨论】:
-
.closest()方法查看元素父母。如果你真的想使用 jquery,你应该试试.prev()。 -
@ry4nolson 没有 jquery 无法解决这个问题... vue 有选项吗?
-
你完全可以在没有 jquery 的情况下做到这一点。请看下面我的回答。您将无效语法与不当 Vue 使用结合在一起。