我有一个html页面。它具有多种形式。
我想要一个通用函数,在提交任何表单后,它将接受该表单的所有输入并将其作为JSON发送到另一台服务器。
据我了解,我想要的是以下内容:
<form enctype="application/json" name="createStudentForm" onsubmit="sendForm()">
Student ID (must be 3 digit number):
<input type="text" name="student_id"><br>
Nickname:
<input type="text" name="student_nickname"><br>
<input type="submit">
</form>
然后:
<script>
function sendForm(){
console.log('sent form');
}
</script>
我的问题是:a)为什么我提交的控制台没有记录下来?当我使用chrome测试时,发送Submit和b)后控制台为空,如何从sendForm()内部访问表单的输入?我需要直接引用该表单吗,还是有一种在我调用它时将数据传递给sendForm()的方法?
遵循迈克尔所说的内容,并使用一些jquery,可以使此工作非常容易。
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
function sendForm(form){
console.log('sent form: ' + $(form).serialize());
return false;
}
</script>
<body>
<form enctype="application/json" name="createStudentForm" onsubmit="return sendForm(this)">
Student ID (must be 3 digit number):
<input type="text" name="student_id"><br>
Nickname:
<input type="text" name="student_nickname"><br>
<input type="submit">
</form>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句