gpt4 book ai didi

javascript - 从控制台使用 javascript 填写并提交表单

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

这是我的表格:

 <form id="myForm">
<input id="htmlString" type="text" name="htmlField" ><br>
<input type="Submit" value="Submit" >
</form>

并且需要从控制台填充它。只是为了在我的应用程序中使用它,将带有数据的 javascript 注入(inject)到本地 html 文件。

我尝试制作没有提交按钮的表单,如下所示:

<body>

<form id="myForm">
<input id="htmlString" type="text" name="htmlField" ><br>
</form>

<script>
htmlString.oninput = function(){
///do some stuff
}

</script>

</body>

期待:

document.getElementById('htmlString').value="moo" ;

它会自动提交表单,因为这里使用了oninput。但它只是充满了输入而没有进一步进行。

尝试过其他解决方案:

form = document.getElementById("myForm")
form.submit()

但是只是刷新了页面,并没有提交表单。

需要的只是一个文件,没有别的,用javascript注入(inject)我的字符串来运行嵌入在html中的函数。

最佳答案

尝试隐藏输入按钮。

<body>

<form id="myForm">
<input id="htmlString" type="text" name="htmlField" ><br>
<input type="Submit" value="Submit" style="display: none" >
</form>
<button onclick="simulateConsole()">Try it</button>

<script>
htmlString.oninput = function(){
if(this.value === "moo") {
myForm.submit();
}
}

// This event will be triggered even if you use console
htmlString.onsubmit = function(){
if(this.value === "moo") {
// do something onSubmit
}
}

function simulateConsole() {
// you can simulate this in console
htmlString.value = "moo";
myForm.submit();
}

</script>

</body>

希望对你有帮助。

关于javascript - 从控制台使用 javascript 填写并提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59050829/

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