[英]Call a function by input type submit with the value of another input element
I have this code that calls a function on key up but I don't want it to be called so often because if you enter a domain name keyup is triggered very often, so I made a submit button but I don't know how I can call the function with the value of the first input tag.. I did that with this.value first but that probably won't work on the other element.我有这段代码在 key up 时调用 function 但我不希望它被如此频繁地调用,因为如果你输入域名 keyup 会经常触发,所以我做了一个提交按钮,但我不知道我是怎么做的可以使用第一个输入标签的值调用 function。我首先使用 this.value 执行此操作,但这可能不适用于其他元素。 Thanks for any help谢谢你的帮助
<form>
Hostname: <input type="text" id="input" onkeyup="ping(this.value)">
<input type="submit">
</form>
how I can call the function with the value of the first input tag..如何使用第一个输入标签的值调用 function ..
You can try querySelector()
that matches the first element:您可以尝试与第一个元素匹配的querySelector()
:
document.querySelector('input[type=text]').value
Demo:演示:
function ping(v){ console.log(v) }
<form> Hostname: <input type="text" id="input"> <input type="submit" onclick="ping(document.querySelector('input[type=text]').value)"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.