在按钮单击共享点上触发 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6204728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:44:01  来源:igfitidea点击:

Firing javascript on button click sharepoint

javascriptsharepointbuttonclick

提问by TomH

All I am trying to accomplish is to call a javascript function when a button is clicked in sharepoint. This is the extent of my 'code' in sharepoint designer 2007...

我想要完成的只是在 sharepoint 中单击按钮时调用 javascript 函数。这是我在 sharepoint 设计器 2007 中的“代码”的范围...

<%@ Page masterpagefile="~masterurl/default.master" language="C#" title="|" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bxe2111e8529c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
<script type="javascript">

function tellme() {
    alert('yep yep yep');
}

</script>
<p></p>
<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />
</asp:Content>

Can anyone tell me why the function is not called? When I save the page, view it and click the button, it just acts as a submit button. I'm perfectly happy with JS/HTML and PHP but I'm dabbling in SharePoint / .net and struggling slightly.

谁能告诉我为什么不调用该函数?当我保存页面、查看它并单击按钮时,它只是充当提交按钮。我对 JS/HTML 和 PHP 非常满意,但我正在涉足 SharePoint/.net 并略有挣扎。

Thanks

谢谢

Tom

汤姆

回答by Fox

The Button you've added in your Sharepoint page is a ASP.Net button. it's default behaviour is postback.

您在 Sharepoint 页面中添加的按钮是一个 ASP.Net 按钮。它的默认行为是回发。

If you want to do something client side, use :

如果你想做一些客户端,使用:

<input type="button" id="ClientSideBtn" value="Click ME" onclick="javascript:tellme()" />

If you want to do something server-side, use:

如果要在服务器端执行某些操作,请使用:

<asp:Button Text="Click ME" id="Button1" runat="server" onclick="Button1_Click"/>

For the server side button , you would need to write some c# or vb.net code:

对于服务器端按钮,您需要编写一些 c# 或 vb.net 代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Do something here

        //Such as
        this.Response.Redirect("http://www.google.com");
    }

You can read more here: http://support.microsoft.com/kb/306459

您可以在此处阅读更多信息:http: //support.microsoft.com/kb/306459

回答by Danny Jessee

Change the line:

更改行:

<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />

To instead read:

改为阅读:

<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme(); return false;" />

By returning false, you will prevent the PostBack.

通过返回false,您将阻止回发。

回答by Ashutosh Singh-MVP SharePoint

use following code

使用以下代码

    <%@ Page masterpagefile="~masterurl/default.master" language="C#" title="|" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bxe2111e8529c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">

<p></p>
<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />

<script type="text/javascript">

function tellme() {
    alert('yep yep yep');
}

</script>

</asp:Content>