我想用JavaScript操作替换我的HTML按钮。虽然我需要一个功能来提交表单以便工作。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>reCAPTCHA | Blazzike</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
$(function(){
$("header").load("HTML/header.html");
$("footer").load("HTML/footer.html");
});
</script>
<script>
var submit = function(){
document.getElementById("rec").submit();
}
</script>
<link rel="stylesheet" type="text/css" href="CSS/style.css">
<style>
#submit {
background: #1a1a1a;
color: #ffffff;
font-size: 25px;
margin: 0px;
padding: 0.7em;
border: 0px;
width: 303px;
min-width: 250px;
}
</style>
</head>
<header></header>
<body>
<center>
<form id="rec" method="POST" action="<?php echo $_GET["r"]; ?>">
<div data-theme="dark" class="g-recaptcha" data-sitekey="<siteKey>" data-callback="submit"></div>
<input value="Continue" type="submit" id="submit">
</form>
</center>
</body>
<footer></footer>
</html>
上面的脚本不起作用,因为我认为JavaScript无法以这种方式提交帖子表单。任何帮助都可以。
谢谢, Blazzike。
答案 0 :(得分:0)
尝试使用javascript来按ID选择元素。确保你把&#34; rec&#34;作为jn html形式的id!
document.getElementById("rec").submit();
编辑1: 使用输入类型=&#34;按钮&#34;而不是提交。然后使用Matt Elliot的第二部分片段:
//Detect submit button click and perform some actions, then submit form
$("#submit").on('click', function() {
//Do whatever you want here //...
$("#rec").submit();
});
答案 1 :(得分:0)
如果您希望用户单击提交按钮,运行Javascript函数并执行某些任务,则使用jQuery提交表单,您可以执行此类操作。
//First prevent form from submitting on button click
$("#rec").on('submit', function(e) {
e.preventDefault();
});
//Detect submit button click and perform some actions, then submit form
$("#submit").on('click', function() {
//Do whatever you want here
//...
$("#rec").submit();
});