2 回答

TA貢獻1806條經(jīng)驗 獲得超5個贊
由于表格中有多行,因此生成的 HTML 中有多個按鈕,您需要使用一種方法來捕獲對要分配事件偵聽器的所有元素的引用 - 除非您選擇使用event delegation但在這種情況下存在沒有必要這樣做。document.querySelectorAll在 vanilla javascript 中捕獲對按鈕的引用似乎很合適 - 并且為了便于forEach在某些瀏覽器中nodelist使用document.querySelectorAll轉(zhuǎn)換為數(shù)組的按鈕。
每個按鈕都被分配了一個匿名(在這種情況下無論如何)函數(shù)來處理click事件 -this匿名函數(shù)中的值是指允許輕松訪問其任何屬性的按鈕 - 這里只是value感興趣
let col=Array.prototype.slice.call( document.querySelectorAll( 'td > input[type="submit"]' ) );
col.forEach( input =>{
input.addEventListener('click',function(event){
alert( 'Value: '+this.value );
});
});

TA貢獻1875條經(jīng)驗 獲得超3個贊
嘗試使用這個。如果出現(xiàn)問題,請告訴我。干杯
//catch html button with js assigned to variable button
let button = document.querySelector('.btn btn-primary');
//listen for the click
button.addEventListener('click', function(){
button.value;
});
// console log the the value of button
console.log(button);
- 2 回答
- 0 關(guān)注
- 114 瀏覽
添加回答
舉報