我有鉤住同樣的方法webcamSendRequestButton
使用vue.js
<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
}
});
}
}
});
它不起作用。我看不到傳遞給ajax調用中的data方法的'buttonValue'。整個對象在標題'value:buttonValue'中缺失。解決方案是可行的。 – zaq
如果你在處理程序代碼的頂部執行'console.log(e.target.value)',你會得到正確的值嗎?你有沒有試過對'v-on:click'指令使用'native'修飾符使其成爲'v-on:click.native'? –
我知道接受的答案是有效的,但如果按鈕的值不再作爲文字數字靜態提供給視圖本身,而是變爲動態變量的值,則該方法不再有效。你可能會發現這是必需的。 –