为window.location的.NET MVC jQuery的相对路径 [英] .NET MVC jQuery relative path for window.location
问题描述
我有一个真正简单的问题,但似乎无法推测出来。
以下不因为MVC生成的URL(包括所有的路由信息)的方式工作。我想路径名只返回虚拟目录路径。
我做的,当用户从下拉列表中的ID被重定向到一个不同的路线。
$(文件)。就绪(函数(){
$('#TransactionIds')。改变(函数(){
document.location = window.location.pathname +/ CeuTransaction /索引/+ $('#TransactionIds)VAL()。
});
});
使用的UrlHelper建设的路径。它会考虑到应用程序相对于Web服务器根目录下的位置。
$(文件)。就绪(函数(){
$('#TransactionIds')。改变(函数(){
document.location ='<%= Url.Action(指数,CeuTransaction)%>'
+'/'
+ $('#TransactionIds)VAL()。
});
});
备选:分成两部分,这样的功能可以包含在一个javascript库(仍需从视图或主调用)。随着工作的一点点你可以把一个jQuery扩展了。
函数redirectOnChange(选择器,动作)
{
$(选择).change(函数(){
document.location =动作+'/'+ $(选择).VAL();
});
}
在该视图:
$(函数(){
redirectOnChange('#TransactionIds',
'<%= Url.Action(指数,CeuTransaction)%>' );
});
I have a real simple problem, but can't seem to figure it out.
The following doesn't work because of the way MVC builds the URL (It includes all the route information). I want pathname to return the virtual directory path only.
All I'm doing is redirecting to a different route when a user selects an ID from a drop down list.
$(document).ready(function() {
$('#TransactionIds').change(function() {
document.location = window.location.pathname + "/CeuTransaction/Index/" + $('#TransactionIds').val();
});
});
Use the UrlHelper to build the path. It will take into account the location of the application relative to the web server root.
$(document).ready(function() {
$('#TransactionIds').change(function() {
document.location = '<%= Url.Action( "Index", "CeuTransaction" ) %>'
+ '/'
+ $('#TransactionIds').val();
});
});
Alternative: split into two parts so that the function can be included in a javascript library (still needs to be invoked from view or master). With a little bit of work you could make it a jQuery extension, too.
function redirectOnChange( selector, action )
{
$(selector).change( function() {
document.location = action + '/' + $(selector).val();
});
}
In the view:
$(function() {
redirectOnChange('#TransactionIds',
'<%= Url.Action( "Index", "CeuTransaction" ) %>' );
});
这篇关于为window.location的.NET MVC jQuery的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!