亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從動態表格單元格獲取值并將其值傳遞給 JS 函數

從動態表格單元格獲取值并將其值傳遞給 JS 函數

慕哥9229398 2023-09-21 10:51:48
我使用 JavaScript 動態創建一個表。我想獲取 的值stationIdCell以便將其傳遞給refreshMETAR()函數。但是,我不知道如何讓它發揮作用。任何幫助,將不勝感激。這是我的桌子const metarTableElement = document.querySelector('#metar_table')const rowCount = metarTableElement.rows.lengthif(rowCount > 6){  metarTableElement.deleteRow(rowCount - 1)} else {  const row = metarTableElement.insertRow(1)  var stationIdCell = row.insertCell(0)  stationIdCell.innerHTML = `<button onclick="refreshMETAR(this.innerHTML)">${station_id}</button>`  //I want to pass this value to the function  var latitudeCell = row.insertCell(1)  latitudeCell.innerHTML = `${latitude}`  var longitudeCell = row.insertCell(2)  longitudeCell.innerHTML = `${longitude}`  var rawMETARCell = row.insertCell(3)  rawMETARCell.innerHTML = `${raw_metar}`這是我想要將單元格值傳遞給的函數:const refreshMETAR = () => {  const stationElement    = this.innerHTML //The value of the table cell should be read here  const station           = stationElement.value  const alertPanelElement = document.querySelector('#alert_panel')  console.log(`entered: ${station}`)  validateADDSStation(station)    .then(response => response.json())    .then(json => {      let icao = null      let site = null      try{        icao = json.response.data[0].Station[0].station_id[0]        site = json.response.data[0].Station[0].site[0]      } catch(err) {        alertPanelElement.classList.remove('w3-hide')        alertPanelElement.classList.add('w3-show')        stationElement.focus()        stationElement.select()        console.log("BAD CODE")      }      console.log(`RETURN VALUE FROM VALIDATE: ${icao}`)      if( station !== icao){        console.log(`ICAO ${icao} not found`)        alertPanelElement.classList.remove('w3-hide')        alertPanelElement.classList.add('w3-show')         })      }    })}
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

看來您正在嘗試將參數 station_id 作為參數發送到函數刷新METAR 中,以便在單擊按鈕時發送請求。您的代碼中有兩個問題。

  1. <button onclick="refreshMETAR(this.innerHTML)">${station_id}</button> 我認為上面的語句中的這將引用窗口,而不是按鈕元素或 stationIdCell 元素。

  2. const refreshMETAR = () => { const stationElement = this.innerHTML //The value of the table cell should be read here 您發送 (this.innerHtml) 作為 params ,但這不是在 param 中使用 a 的方法

請嘗試這個:

stationIdCell.innerHTML = `<button onclick="refreshMETAR('${stationId}')">${station_id}</button>`


const refreshMETAR = (stationId) => {

    const station = stationId

    console.log(`entered: ${station}`)


查看完整回答
反對 回復 2023-09-21
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

事件委托典型案例


第一部分:



// GLOBAL

const metarTableElement = document.querySelector('#metar_table')

 

document.addEventListener('click', refreshMETAR )



//...

let rowCount = metarTableElement.rows.length

if (rowCount > 6)

  {

  metarTableElement.deleteRow( --rowCount )

  }

else 

  {

  let row = metarTableElement.insertRow()


  row.insertCell().innerHTML   = `<button class="cl_METAR">${station_id}</button>`

  row.insertCell().textContent = latitude

  row.insertCell().textContent = longitude

  row.insertCell().textContent = raw_metar

//...

點擊按鈕...


function refreshMETAR(evt)

  {

  if (!evt.target.matches('button.cl_METAR')) return  // reject clicks from somewhere else 


  const station =  evt.target.textContent // === value in ${station_id}


  // ...


  }


查看完整回答
反對 回復 2023-09-21
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號