Javascript 使用onsubmit事件获取查询字符串
我想将表单提交转换为ajax请求。这是我的密码:Javascript 使用onsubmit事件获取查询字符串,javascript,ajax,onsubmit,Javascript,Ajax,Onsubmit,我想将表单提交转换为ajax请求。这是我的密码: <html> <head> <meta charset="iso8859-9"> <script> document.addEventListener("DOMContentLoaded", function(event) { document.getElementById("query_form").onsubmit = function(event) { cons
<html>
<head>
<meta charset="iso8859-9">
<script>
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("query_form").onsubmit = function(event) {
console.log(event);
return false;
}
});
</script>
</head>
<body>
<form action="query.php" id="query_form" method="get">
<input name="first">
<input name="second">
<button type="submit">
</form>
<table id="results"></table>
</body>
</html>
document.addEventListener(“DOMContentLoaded”),函数(事件){
document.getElementById(“查询表单”).onsubmit=函数(事件){
console.log(事件);
返回false;
}
});
我可以在onsubmit事件中读取计算出的查询字符串,将其用于ajax,还是需要自己使用javascript创建它。如果可能的话,我不想使用外部库
我可以在onsubmit事件中读取计算出的查询字符串以将其与ajax一起使用吗
没有
我需要自己使用javascript创建它吗
对
一般解决方案包括在event.target.elements、集合名称/值对上循环,同时过滤掉字段集、禁用的元素、未选中的复选框/单选按钮,正确处理多个选择,然后通过encodeURIComponent(name)+“=”+encodeURIComponent(value)传递所有结果
加入之前,请在和上加入
我可以在onsubmit事件中读取计算出的查询字符串以将其与ajax一起使用吗
没有
我需要自己使用javascript创建它吗
对
一般解决方案包括在event.target.elements、集合名称/值对上循环,同时过滤掉字段集、禁用的元素、未选中的复选框/单选按钮,正确处理多个选择,然后通过encodeURIComponent(name)+“=”+encodeURIComponent(value)传递所有结果
加入之前,请在和上加入