首页 > 解决方案 > 如何使用 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>

标签: javascriptphphtml

解决方案


感谢@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>

推荐阅读