如何使用 vanilla javascript 在点击时获取输入值?
function getvalue() {
console.log(this.val);
}Run Code Online (Sandbox Code Playgroud)
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>Run Code Online (Sandbox Code Playgroud)
function getvalue(t) {
console.log(t.value);
}Run Code Online (Sandbox Code Playgroud)
<input onclick="getvalue(this)" value="asdf"></input>
<input onclick="getvalue(this)" value="asdf2"></input>Run Code Online (Sandbox Code Playgroud)
在函数调用中使用 event.target.value
当函数被调用时,event对象被传递给函数。event.target标识调用该函数的元素。
function getvalue() {
console.log(event.target.value);
}Run Code Online (Sandbox Code Playgroud)
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20640 次 |
| 最近记录: |