Basic Custom List: How to redirect to different page on Cancel and on Save button click in NewForm.aspx?
-
11-10-2020 - |
题
I have a basic custom list in SharePoint Online where users have to add items, and I want to make sure that:
If user clicked Save, then, User sees: Thanks-Page
If user clicked Cancel, then, User sees: Bye-Page
How can I accomplish this with script editor web part on the NewForm.aspx page? Is there a better and fast way?
解决方案
On your Cancel button, in the NewForm.aspx page, you can add the following onclick function:
<button type="button" onclick="goToByePage();">Cancel</button>
<script>
function goToByePage(){
window.location.assign("https://DOMAIN/PAGEREF/Bye-Page.aspx");
}
</script>
The save button is a little more difficult - normally it automatically refreshes the page with the new item shown in the list. Instead, you want it to re-direct to a thanks page. You could use Marc Anderson's SPServices function SPRedirectWithID.
其他提示
I would recommend that rather than Script Editor or editing the page directly that you actually add a Content Editor Webpart. Have it point to a .HTML file that you put in your SiteAssets directory. In the Content Link section of the Content Editor Webpart you can then include the HTML & JavaScript.
To overcome the Save it might actually be easier to just create the link to the list (new form) by creating a link on your site and passing a different source.
IE: if you have a button on the page that goes to http://yourfarm.sharepoint.com/sites/yoursite/Lists/YourList/NewForm.aspx?&Source=/sites/yourSite/Pages/Thanks.aspx
That way when they save it'll dump them off there. The cancel you could do a little bit of jQuery to change the function. I think the one item she was missing is you need to have that function but then there needs to be some code that overrides the default cancel.
<script>
$("input[value='Cancel']").attr("onClick", "goToByePage()");
</script>