javascript - 我需要根据 html 表单提交的响应进行重定向

标签 javascript html

例如

<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/

相关文章:

javascript - 有没有办法结合 jQuery/Javascript 中的事件?

Javascript:toString(36) 用于大整数

javascript - ul 中有多个链接,但只有第一个链接用于点击事件

javascript - 具有不同高度的响应式图片库 - 如何使用 css(或 jquery)实现这一点

javascript - 如何在不使用ajax的情况下发送POST请求

java - 无法找到双引号来结束字符串

javascript - 单击按钮时增加 CSS 属性

jQuery 验证仍然提交无效字段

java - 如何在GWT UiBinder中使用JavaScript回调函数?

javascript - 相对于鼠标位置从其中心缩放图像