gpt4 book ai didi

javascript - 使用 onclick 从输入中获取值

转载 作者:行者123 更新时间:2023-11-29 16:05:58 24 4
gpt4 key购买 nike

如何使用 vanilla javascript 获取点击输入的值?

function getvalue() {
console.log(this.val);
}
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>

最佳答案

在函数调用中使用 event.target.value

当函数被调用时,事件 对象被传递给函数。 event.target 标识哪个元素调用了函数。

function getvalue() {
console.log(event.target.value);
}
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>

关于javascript - 使用 onclick 从输入中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42177441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com