可以將表單中的數據通過location.search的方法傳遞到URL中,然后再通過JavaScript獲取URL中的參數來處理表單數據。例如,可以通過以下方式將表單數據傳遞到URL中:
<form id="myForm">
<input type="text" name="name" id="name">
<input type="email" name="email" id="email">
<button type="submit" onclick="submitForm()">Submit</button>
</form>
<script>
function submitForm() {
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var queryParams = `?name=${name}&email=${email}`;
window.location.search = queryParams;
}
</script>
<script>
window.onload = function() {
var searchParams = new URLSearchParams(window.location.search);
var name = searchParams.get('name');
var email = searchParams.get('email');
document.getElementById('name').value = name;
document.getElementById('email').value = email;
}
</script>
這樣就可以通過表單提交數據并將數據傳遞到URL中,然后在頁面加載時獲取URL中的參數并處理表單數據。