简体   繁体   English

JavaScript重定向连接了url而不是重定向

[英]Javascript redirect concatenates url linstead of redirecting

I want to call a controller action whenever a value is updated from a textbox. 每当从文本框中更新值时,我都想调用控制器动作。

Note that this script sits inside the same view which will be returned from the Dispatch/Index controller action 请注意,此脚本位于同一视图中,该视图将从Dispatch / Index控制器操作返回

I tried doing it like this: 我尝试这样做:

<script>
    $('#deliveryDate').change(function () {
        var date = $('#deliveryDate').val();
        var sDate = date.split('/');
        location.href = 'dispatch/' + sDate[0] + sDate[1] + sDate[2];
    });
</script>

And it works perfectly the first time the value changes (ie, the page redirects to /dispatch/12122016 ). 并且在值第一次更改时(即页面重定向到/dispatch/12122016 ),它可以完美地工作。

However, when I change the value again, it redirects to /dispatch/dispatch/13122016 , so the value just keeps concatanating and this of course produces an error. 但是,当我再次更改该值时,它会重定向到/dispatch/dispatch/13122016 ,因此该值一直保持连接状态,这当然会产生错误。


I tried chaging the redirect line to location.href = '@Url.Action("Index", "Dispatch")/' + sDate[0] + sDate[1] + sDate[2]; 我尝试将重定向行location.href = '@Url.Action("Index", "Dispatch")/' + sDate[0] + sDate[1] + sDate[2];location.href = '@Url.Action("Index", "Dispatch")/' + sDate[0] + sDate[1] + sDate[2]; , but now it just concatenates the new date on to the url instead of redirecting how I need it to... (ie, it navigates to /dispatch/12122016/13122016 ) ,但现在它只是将新日期连接到url上,而不是将我需要的日期重定向到...(即,它导航到/dispatch/12122016/13122016

Is there ANY way of doing this without including the literal url path?? 有没有做到这一点的任何方式,而无需包括原义的URL路径?

How can "clear" the current url before redirecting the action I need (which should just be dispatch/12122016 ? 在重定向我需要的操作之前,应该如何“清除”当前网址(应该只在dispatch/12122016

Simply prepend a forward slash in the URL, this should always dictate that the url change is always bound to be absolute. 只需在URL前面加一个正斜杠,就应该始终表明url的变化始终是绝对的。

As a bonus: I also changed your date concatenation after /dispatch into a join. 作为奖励:在/dispatch之后,我还将您的日期串联更改为联接。

$('#deliveryDate').change(function () {
    var date = $('#deliveryDate').val();
    location.href = '/dispatch/' + date.split('/').join('');
});

Try this by appending the forward slash. 通过添加正斜杠来尝试此操作。 Because your concatenating the dispatch string on every redirect 因为您在每个重定向上连接调度字符串

<script>
    $('#deliveryDate').change(function () {
        var date = $('#deliveryDate').val();
        var sDate = date.split('/');
         Var URL =  '/dispatch/' + sDate[0] + sDate[1] + sDate[2];
        Window.location.href = URL
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM