例如
<form onsubmit="submitForm(event)">
<p> Do you like cats or dogs </p>
<input type="radio" id="cat" value="Cats">
<label for="cat">Cats</label>
<input type="radio" id="dog" value="Dogs">
<label for="dog">Dogs</label><br>
<button>Submit</button>
</form>
<script>
function submitForm(event) {
if(document.getElementById('cat').checked) {
//(send the user to the cat page)
}else if(document.getElementById('dog').checked) {
//(send the user to the dog page)
}
</script>
如何根据用户对我的问题的回答将他们发送到与我的网站位于同一根文件夹中的不同页面
最佳答案
尝试一下window.location.href
<form onsubmit="submitForm(event)">
<p> Do you like cats or dogs </p>
<input type="radio" id="cat" value="Cats">
<label for="cat">Cats</label>
<input type="radio" id="dog" value="Dogs">
<label for="dog">Dogs</label><br>
<button>Submit</button>
</form>
<script>
function submitForm(event) {
if(document.getElementById('cat').checked) {
//(send the user to the cat page)
window.location.href = 'yoursite.com/cat'
}else if(document.getElementById('dog').checked) {
//(send the user to the dog page)
window.location.href = 'yoursite.com/dog'
}
</script>
关于javascript - 我需要根据 html 表单提交的响应进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73116997/