jQuery 如何从 asp.net 按钮单击调用 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15136034/
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-26 14:26:37  来源:igfitidea点击:

How do I call javascript from asp.net button click?

javascriptjqueryasp.netajax

提问by Neo

I'm working on asp.net and want to execute following javascript code. I'm using VS2010.

我正在使用 asp.net 并希望执行以下 javascript 代码。我正在使用 VS2010。

   <title></title>
<script type="text/javascript">
    function myClosure() {
        var canyousee = "here I'm ";
        return (function theClosure() {
            return { canyouseeIt: canyousee ? "yes" : "no" };
        });
    }
    var closure = myClosure();
    closure().canyouseeIt;
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myClosure();" />
    </div>

How do i execute function myClosure()on button click so that It gives me confirmi.e popup for yesor no?

我如何执行function myClosure()上按一下按钮,这样它给了我confirm即弹出的yes还是no

  1. Which code I need to put for confirm?
  2. How can I execute it on Button Click?
  1. 我需要输入哪个代码confirm
  2. 我怎样才能执行它Button Click

thanks

谢谢

采纳答案by jbl

I hope this can be of some help. I must confess that what you want to achieve is not clear to me.

我希望这会有所帮助。我必须承认,你想要达到的目标我不清楚。

<title></title>
<script type="text/javascript">
     myClosure=function() {
        var canyousee = "here I'm ";
        return (function () {
            return { canyouseeIt: function(){return confirm (canyousee)}};
        });
    }
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="(myClosure())().canyouseeIt()" />
    </div>

回答by Ahmed Abdullah Saeed

<html>
<head>
<script type="text/javascript">
function myClosure(){
          var r=confirm("Press a button!")
        if (r==true)
        {
             alert("You pressed OK!")
             return true;
        }
        else
        {
              alert("You pressed Cancel!");
              return false;
        }

}
</script>
</head>

<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myClosure();" />
</div>
</body>
</html>

回答by Hirav Sampat

use

onclientclick="javascript:return YourFunction();"

<script> 
function YourFunction() {
//Do your stuff

return true;
}
</script>

If you return true your asp.new onclick event will be called.. If you return false it wont be called..

如果你返回 true 你的 asp.new onclick 事件将被调用.. 如果你返回 false 它不会被调用..

回答by Aristos

as you call it, but prevent the post back by return false on

正如你所说,但通过 return false on 阻止回帖

OnClientClick="myClosure();return false;"

I do not know nether understand the rest of your logic, but this is your issue right now.

我不知道你是否理解你的其余逻辑,但这是你现在的问题。