如何使用jQuery将最终用户重定向到另一个网页

3

是否可以在复选框被勾选/取消勾选时重定向最终用户?如果可以,请问如何实现?

我尝试使用以下代码通过get方法来重定向他

HTML

<form><input type="checkbox" name="fixee" value="true" style="margin-left:40px;margin-right:3px;" /><input type="checkbox" name="nonFixee" value="true" style="margin-left:10px;margin-right:3px;" /></form>

jQuery

$("input[type='checkbox']").change(function () {
    if ($("input[name='fixee']").prop('checked') && $("input[name='nonFixee']").prop('checked')) {
        alert('2');
    }
    else if ($("input[name='fixee']").prop('checked') && $("input[name='nonFixee']").prop('checked')) {
        alert('3');
    }
    else{
        alert('1');
    }
});

http://jsfiddle.net/eKa8S/9/

我在Visual Studio中编写代码,~表示项目的根目录。

请问有什么好的建议吗?


你尝试过window.location.href吗? - markpsmith
这里有一个可能会有所帮助的堆栈溢出帖子。https://dev59.com/_3RB5IYBdhLWcg3wz6Wd - Jeremy Quinton
1
@markpsmith,你能给我展示一下这是如何工作的吗? - Lucie kulza
请查看以下链接,它们可能会对您有所帮助。如何在jQuery / JavaScript中创建重定向页面?如何在jQuery Ajax调用后管理重定向请求 - Gaurav Dhavale
3个回答

0

你的示例已经修改以执行get请求:

$("input[type='checkbox']").change(function () {
    if ($("input[name='fixee']").attr("checked") && !$("input[name='nonFixee']").attr("checked")) {
        window.location = "~/Alertes/Index?fixee=true&nonFixee=false";
    }
    else if (!$("input[name='fixee']").attr("checked") && $("input[name='nonFixee']").attr("checked")) {
        window.location = "~/Alertes/Index?fixee=false&nonFixee=true";
    }
    else {
        window.location = "~/Alertes/Index?fixee=true&nonFixee=true";
    }
});

小提琴


0

试试这个,

$("input[type='checkbox']").change(function () {
    if ($("input[name='fixee']").prop('checked') && $("input[name='nonFixee']").prop('checked')) {
        window.location.href = '@Url.Action("Action1", "Controller")'
    }
    else if ($("input[name='fixee']").prop('checked') && $("input[name='nonFixee']").prop('checked')) {
         window.location.href = '@Url.Action("Action2", "Controller")'
    }
    else{
        window.location.href = '@Url.Action("Action3", "Controller")'
    }
});

0

您可以使用 window.location = "~/Alertes/Index?fixee=true&nonFixee=true"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接