【发布时间】:2020-09-13 02:42:50
【问题描述】:
我有一个如下所示的提交表单:
<form id="form_id" method="POST" action="https://127.0.0.1:8000/api/node">
<h1>Add Node</h1>
<h4>Node ID</h4>
<div class="name">
<input type="text" name="name" placeholder="node id" id="node_id" />
</div>
<h1>Get the latitude and longitude of the node</h1>
<p>
latitude: <span id="latitude"></span><br />
longitude: <span id="longitude"></span>
</p>
<style>
#node1Map { height: 260px; }
</style>
<div id="node1Map"></div>
<script>
const mymap = L.map('node1Map').setView([0, 0], 1);
const attribution ='©: <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const tiles = L.tileLayer(tileUrl, { attribution });
tiles.addTo(mymap);
// get coordinate
var latitude, longitude, marker;
mymap.on('click', function(e) {
if(marker) mymap.removeLayer(marker);
latitude = e.latlng.lat;
longitude = e.latlng.lng;
console.log(latitude);
console.log(longitude);
marker = L.marker([latitude, longitude]).addTo(mymap);
document.getElementById('latitude').textContent = latitude;
document.getElementById('longitude').textContent = longitude;
});
</script>
<h4>Location Address</h4>
<input type="text" id="location" />
<div class="btn-block">
<button type="submit" href="/">Submit</button>
</div>
</form>
</body>
</html>
我现在的情况是: 1. 我提交表单,然后表单中的数据将保存在我的 django rest API (https://127.0.0.1:8000/api/node) 2.页面重定向到我在https://127.0.0.1:8000/api/node中的django rest API页面。显示我保存的 json 数据。
但我现在想要的是: 1. 我提交表单,然后表单中的数据将保存在我的 django rest API (https://127.0.0.1:8000/api/node) 2.页面没有重定向到我在https://127.0.0.1:8000/api/node中的django rest API。但重定向到我想要的另一个网址。例如:页面将重定向到https://127.0.0.1:8000/listdata。
我该怎么做?
【问题讨论】:
-
在第三步(成功后)设置一个变量,如 'success = 1' 并在 .../api/node 设置 if 语句以检查是否成功 = 1,如果是则重定向到 . .../列表数据
标签: javascript html jquery forms post