JavaScript函数不返回值 [英] Javascript function does not return value

查看:97
本文介绍了JavaScript函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主页上有一个按钮(授权),该按钮引发事件.然后,基于子页面,我授权票证.但是,当有人单击授权按钮时,我也必须进行确认,然后禁用授权按钮,以避免多次单击.

这是母版页中的HTML

I have a button (authorise) in master page, which raises event. Then based on child page, I authorise the ticket. However when someone clicks on authorise button, I have to confirm as well and then disable authorise button to avoid multiple clicks.

Here is HTML in masterpage

<asp:ImageButton ID="imgAuthorise" runat="server" ImageUrl="~/Images/Authorise.png" TabIndex="-1" ToolTip="Authorise" Visible="False" OnClientClick="return ToggleButton()" />


JavaScript方法


JavaScript Method

function ToggleButton() {
            var button = document.getElementById(event.srcElement.id);
            var returnValue = true;

            if (button.disabled) {
            }

            else {
                var check = confirm('Are you sure that you want to authorise?');
                if (check) {
                     button.disabled = true;
                    returnValue = true;
                }
                else {
                     button.disabled = false;
                    returnValue = false;
                }
            }
            return returnValue;
        }


现在,如果我用button.disabled注释行.函数返回正确的值,并且子页面成功执行事件.但是,如果我将button.disabled行放回去,则子页面不会订阅事件.

有人可以帮我吗?


Now, if I comment lines with button.disabled. function returns proper value and child page executes event successfully. However, if I place button.disabled lines back, then child page does not subscribe to event.

Can anyone help me please?

推荐答案

可以在调试器下运行它,也可以将其夹在try-catch块中以查看会发生什么.很可能找不到button元素,并且尝试取消引用未初始化的对象会导致异常.

—SA
Either run it under debugger or sandwich in try-catch block to see what happens. Most likely, the button element is not found, and the attempt to dereference non-initialized object causes exception.

—SA


hiii,

这是您的母版页代码,母版页上的授权"按钮希望它会回答.

hiii,

this is your master page code where authorise button on master page hope this will ans.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <script type="text/javascript">
         function ToggleButton() {
             var button = document.getElementById('<%= imgAuthorise.ClientID %>');
             var returnValue = true;
             if (button.disabled) {
             }

             else {
                 var check = confirm('Are you sure that you want to authorise?');
                 if (check) {

                     returnValue = true;
                 }
                 else {

                     returnValue = false;
                 }
             }
             return returnValue;
         }


    </script>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:ImageButton ID="imgAuthorise" runat="server" ImageUrl="~/Images/Authorise.png" TabIndex="-1" ToolTip="Authorise"  OnClientClick="return ToggleButton()" />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


嗨!
谢谢您的帮助.我找到了解决办法
选中此链接
在我的情况下,它可以完美运行.


干杯
Hi!
Thanks for the help. I found solution
Check this link
It works perfectly in my situation.


Cheers


这篇关于JavaScript函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆