javascript - 如何使用 JS 自动提交表单
问题描述
我想在页面完成加载后使用 JS 添加代码自动提交表单 我也需要它提交输入值 当我单击手动自动工作但未使用输入“g-recaptcha-response”值提交时,提交和验证码响应效果很好<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $captcha_site_key ?>"></script>
</head>
<form action="check.php" id="myform" method="POST">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="Submit" name="Submit">
</form>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $captcha_site_key ?>', {action:'validate_captcha'})
.then(function(token) {
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>
<script type="text/javascript">
window.onload = function(){
document.getElementById("myform").submit();
}
</script>
</body>
</html>
解决方案
感谢@Mosia Thabo 帮助我
JS代码是
解释:-首先setAttribute('value', 'token');
然后等待1秒然后提交myform
<script type="text/javascript">
document.getElementById('g-recaptcha-response')..setAttribute("value", token);
</script>
<script type="text/javascript">
window.onload=function(){
window.setTimeout(document.myform.submit.bind(document.myform), 100);
};
</script>
推荐阅读
- visual-studio-code - 从 vscode 中删除彩色缩进
- swift - 非最终类中的方法 ... 无法在协议扩展中实现,因为它返回“Self”并具有相关的类型要求
- git - 更改 git 分支类型
- django - 如何呈现由 CreateView 自动创建的表单(在模板标签中)
- jsf - java.lang.illegalargumentexception 无法识别的内容类型
- javascript - 我是否为自定义 Wordpress 主题错误地将样式表排入队列?
- excel - 如果单元格为空白,则清除 VBA 中另一个单元格代码的内容
- python - 在数据框中解压数据的方法
- mongodb - MongoDB中基于光标的分页,对2个属性进行排序
- python - 用 Tensorflow-gpu 替换我的正常张量的符号张量