我在一个页面上有几个按钮,它们连接到相同的方法webcamSendRequestButton

<button v-on:click="webcamSendRequestButton" value="0" type="button" class="webcam-send-request-button" :disabled="disabled">Verify</button>
<button v-on:click="webcamSendRequestButton" value="1" type="button" class="webcam-send-request-button" :disabled="disabled">Verify</button>
<button v-on:click="webcamSendRequestButton" value="2" type="button" class="webcam-send-request-button" :disabled="disabled">Verify</button>
<button v-on:click="webcamSendRequestButton" value="3" type="button" class="webcam-send-request-button" :disabled="disabled">Verify</button>

点击按钮时,我正在进行一个ajax调用.在jquery或JS中,当使用$(this).val();单击按钮时,获取按钮的值非常简单

在vue中单击按钮时,如何获取其值?

var app = new Vue({
    el: '#my-div',
    methods: {
        webcamSendRequestButton: function() {

            const buttonValue = ??? // How do I fetch the value over here

            $.ajax({
                url: "someurl",
                type: "POST",
                data: {
                    value: buttonValue
                },
                success: function (data) {
                    // Omitted for brevity
                }
            });
        }
    }
});

推荐答案

您可以简单地将其作为函数的参数.

<button v-on:click="webcamSendRequestButton(0)"  type="button" class="webcam-send-request-button" :disabled="disabled">Verify</button>

JS

...
methods: {
  webcamSendRequestButton(value){
     //"value" is the clicked button value
  }
}
...

Vue.js相关问答推荐

如何修复IntersectObserver导致我的OpenLayers Web应用程序中内存泄漏的问题?

将Vite Vuejs应用部署到Apache服务器

写入计算值时出错,但我没有写入任何值

vuejs3 youtube-plugin YT 未定义控制台错误

Svelte 中的简单react 性实验以意想不到的方式失败了. Vue 等效项没有

Nuxt 2.15.7 的构建错误 - 无法解析 CSS @font-face URL

[Vue 警告]:渲染函数中的错误:TypeError:无法读取属性 'first_name' of null

是否可以在没有 Vue 的情况下使用 Vuex?

Vue.js 过滤数组

如何从 vue.js 中的自定义组件中冒泡点击事件?

Vue-test-utils:在单个测试中多次使用 $nextTick

如何使用 JavaScript 按索引删除数组元素?

即使在生产模式下构建,VueJS 也会显示开发模式开发模式消息

Vue JS 在渲染前等待数据

VueJS 2 在多个组件上debounce

Vue cli 在 css 中构建生产更改不透明度

依赖关系甚至在 package.json 和 node_modules 中都没有定义

如何设置 :id 前缀字符串?

如何从扩展面板(Vuetify)中删除阴影?

VueJS 按键查找元素