首页 > 解决方案 > 如何通过触发点击两个函数来发送相同的输入值?

问题描述

我想将相同的输入值发送到两个函数,但它不起作用。让我展示一下我的代码摘要。

const btn = document.querySelector('.btn')

btn.addEventListener('click',getTogether)


function getTogether(event){

    event.preventDefault();

const country = document.querySelector('.input-text').value ;

    getChart(country);
    getGlobal(country)
}

function getChart(country){
......
}

function getGlobal(country){
......
}

是否可以将相同的输入值作为参数发送给两个函数,或者有没有更好的方法?请告诉我。

标签: javascriptfunctiondom-events

解决方案


我不确定你的问题是什么,但下面的代码有帮助吗?

const btn = document.querySelector('.btn')
const country = document.querySelector('.input-text')

btn.addEventListener('click', getTogether)

function getTogether(event) {
  event.preventDefault()
  getChart(country.value)
  getGlobal(country.value)
}

function getChart(country) {
  console.log("getChart called with:", country)
}

function getGlobal(country) {
  console.log("getGlobal called with:", country)
}
<input type="text" class="input-text" />
<button class="btn">Click Me</button>


推荐阅读