使用javascript重定向页面会将值连接到URL,而不是重定向?
我有一个下拉列表,允许用户选择要查看的数据源。用户第一次选择URL时,我的URL如下所示:使用javascript重定向页面会将值连接到URL,而不是重定向?,javascript,jquery,url,asp.net-mvc-4,Javascript,Jquery,Url,Asp.net Mvc 4,我有一个下拉列表,允许用户选择要查看的数据源。用户第一次选择URL时,我的URL如下所示: http://localhost/DataFeeds/Viewer/1 http://localhost/DataFeeds/Viewer/1/2 $('select#ID').change(function () { window.location.replace("@Url.Action("Viewer", "DataFeeds")?id=" + $(thi
http://localhost/DataFeeds/Viewer/1
http://localhost/DataFeeds/Viewer/1/2
$('select#ID').change(function () {
window.location.replace("@Url.Action("Viewer", "DataFeeds")?id=" + $(this).val());
});
在第二个选项中,它如下所示:
http://localhost/DataFeeds/Viewer/1
http://localhost/DataFeeds/Viewer/1/2
$('select#ID').change(function () {
window.location.replace("@Url.Action("Viewer", "DataFeeds")?id=" + $(this).val());
});
这显然是不对的。应将1
替换为2
下面是导致它的代码:
$('select#ID').change(function () {
window.location.replace("@Url.Action("Viewer", "DataFeeds")/" + $(this).val());
});
我已经试过了
window.location.href=“@Url.Action(“查看器”、“数据源”)/”+$(this.val()代码>
但这也是同样的道理
感谢所有建议。以下回复是错误的。window.location.replace()会重定向,我的错
实际上,window.location.replace()与window.location=url相同,只是有一点不同,即replace()从浏览器历史记录中删除了旧位置,因此无法使用“后退”按钮
错误响应:
您正在替换而不是分配
window.location = window.location.replace("@Url.Action("Viewer", "DataFeeds")/" + $(this).val());
// This does not redirect it just grabs the string of the location and modifies it
window.location.replace("@Url.Action("Viewer", "DataFeeds")/" + $(this).val());
// is the same as doing
var redirectTo = window.location.replace("@Url.Action("Viewer", "DataFeeds")/" + $(this).val());
// you still need to do
window.location = redirectTo;
然而
如果它不能像你说的那样工作,那么替换(“@Url.Action(“Viewer”,“DataFeeds”)/”+$(this.val());有缺陷。关于堆栈溢出,有另一个答案可以回答这个问题。我遇到了同样的问题,发现我需要使用这里提到的?name=语法:
而不是使用这个:
$('select#ID').change(function () {
window.location.replace("@Url.Action("Viewer", "DataFeeds")/" + $(this).val());
});
尝试使用类似以下内容:
http://localhost/DataFeeds/Viewer/1
http://localhost/DataFeeds/Viewer/1/2
$('select#ID').change(function () {
window.location.replace("@Url.Action("Viewer", "DataFeeds")?id=" + $(this).val());
});
使用?name=语法时,我不再将结尾连接起来。看起来您是在连接两个字符串,而不是替换1。尝试使用RegExp或类似工具替换该url。在您的示例中,url的值是多少?你能放一些调试器语句进去看看在那一点上发生了什么吗?没有价值。。。这只是一个例子,虽然我想这不是显而易见的,老板?此方法重定向到DataFeeds/undefined
.Nope..更改为document.location:)改变什么?在谈论代码时,含糊不清根本不起作用。您的第一个建议和第二个建议重定向到DataFeeds/undefined
。没关系,我的firebug出现了问题,我说的是从window.location切换到document.location..但没关系