2011-09-06 108 views
0

我可以让代码弹出警告但重定向不起作用。添加一个项目后,它应该重定向。javascript - 重定向

<script type="text/javascript" src="/_layouts/jquery/jquery-1.3.2.min.js"></script> 
<script type="text/javascript"> 
fields = init_fields(); 
// Where to go when cancel is clicked 
goToWhenCanceled = '/test/English/YouCanceled.aspx'; 

// Edit the redirect on the cancel-button's 
$('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){ 
    $(this).click(function(){ 
      STSNavigate(goToWhenCanceled); 
     }) 
}); 

// Edit the form-action attribute to add the source=yourCustomRedirectPage 
function setOnSubmitRedir(redirURL){ 
var action = $("#aspnetForm").attr('action'); 
var end = action.indexOf('&'); 
    if(action.indexOf('&')<0){ 
     newAction = action + "?Source=" + redirURL; 
    }else{ 
     newAction = action.substring(0,end) + "&Source=" + redirURL; 
    } 
$("#aspnetForm").attr('action',newAction); 
alert(redirURL); 
} 

/* 
// Use this for adding a "static" redirect when the user submits the form 
$(document).ready(function(){ 
    var goToWhenSubmitted = '/test/English/ThankYou.aspx'; 
    setOnSubmitRedir(goToWhenSubmitted); 
}); 
*/ 

// Use this function to add a dynamic URL for the OnSubmit-redirect. This function is automatically executed before save item. 
function PreSaveAction(){ 
// Pass a dynamic redirect URL to the function by setting it here, 
// for example based on certain selections made in the form fields like this: 

    var dynamicRedirect = '/surveys/Pages/ThankYou.aspx'; 

    // Call the function and set the redirect URL in the form-action attribute 
    setOnSubmitRedir(dynamicRedirect); 
    alert(dynamicRedirect); 
    // This function must return true for the save item to happen 
    return true; 
} 

function init_fields(){ 
    var res = {}; 
    $("td.ms-formbody").each(function(){ 
     if($(this).html().indexOf('FieldInternalName="')<0) return; 
     var start = $(this).html().indexOf('FieldInternalName="')+19; 
     var stopp = $(this).html().indexOf('FieldType="')-7; 
     var nm = $(this).html().substring(start,stopp); 
     res[nm] = this.parentNode; 
    }); 
    return res; 
} 
</script> 

回答

4

如果你在任何时候设置了window.location.href = 'SomeUrl',它应该重定向到那么。看着你的代码,我没有看到任何地方。

您在什么时候尝试重定向?

+0

我以为这条线正在做重定向。 $( “#aspnetForm”)ATTR( '行动',newAction)。 –

+0

@玛丽河,没有window.location是做它的标准方式。 – riwalk

+0

正确。使用您的jQuery版本,只需更改表单提交时发布的表单操作即可。实际上没有行动正在执行。即使在SharePoint 2010中也有 – Tejs