没有从onclick调用Javascript函数

时间:2011-12-23 03:49:09

标签: javascript asp.net onclick webmethod

这让我把头发拉了出来。网站上的按钮有onclick=method(),而且它没有调用方法。该方法应该获取所有复选框,检查其已检查状态并使用true / false填充chk[]数组。然后WebMethod接受该数组,将其分解为三个较小的数组并对组合运行检查。据我所知,按钮从不调用方法开始。

aspx页面:

<fieldset id="Fieldset">
     <button onclick="SendForm();">Send</button>
     &nbsp;
     <button onclick="CancelForm();">Cancel</button>
</fieldset>
</form>
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />

<script type="text/javascript">
    function SendForm() {
        var email = $get("txtEmail").value;
        var elLength = form1.elements.length;
        var chk = new [42];
        for (i = 0; i < elLength; i++) {
            var count = 0;
            var type = form1.elements[i].type;
            if (type == "checkbox") {
                if (form1.elements[i].checked) {
                    chk[count] = true;
                }
                else {
                    chk[count] = false;
                }
                count++;
            }
            else {
            }
        }
        PageMethods.SendForm(email, chk, OnSucceeded, OnFailed);
    }
</script>

它正在调用的代码隐藏方法:

[WebMethod]
public static void SendForm(string email, bool[] chk)
{
    bool[] desc = new bool[14];
    bool[] loc = new bool[14];
    bool[] other = new bool[14];
    for (int i = 0; i < 14; i++)
    {
        int count = i * 3;
        desc[i] = chk[count];
        loc[i] = chk[count + 1];
        other[i] = chk[count + 2];

    }
    if (string.IsNullOrEmpty(email))
    {
        throw new Exception("You must supply an email address.");
    }
    else
    {
        if (IsValidEmailAddress(email))
        {
            for (int i = 0; i < 14; i++)
            {
                if (desc[i])
                {
                    if ((loc[i]) && (other[i]))
                    {
                        throw new Exception("Invalid, two parameters selected");
                    }
                    else if (loc[i])
                    {
                        // do stuff
                    }
                    else if (other[i])
                    {
                       // do stuff
                    }
                    else
                    {
                        throw new Exception("Invalid, every exemption must have at least one reason selected");
                    }
                }
                else
                {
                    throw new Exception("No exemptions have been selected");
                }
            }
        }
        else
        {
            throw new Exception("You must supply a valid email address.");
        }
    }
}

EDIT !!: 使用以下脚本而不是之前的脚本运行页面就像魅力一样。不知道为什么之前没有用。

<script type="text/javascript">
    function SendForm() {
        var email = $get("txtEmail").value;
        var elLength = form1.elements.length;
        for (i=0;i< elLength;i++) {
            var type = form1.elements[i].type;
            if (type == "checkbox" && form1.elements[i].checked) {
                alert("true!");
            }
            else {
                alert("false!");
            }
        }
        PageMethods.SendForm(email, chk, OnSucceeded, OnFailed);
    }
</script>

3 个答案:

答案 0 :(得分:0)

而不是像

那样调用你的函数
 <button onclick="SendForm();">Send</button>

尝试像这样调用它

<button onclick="javascript:return SendForm();">Send</button>

答案 1 :(得分:0)

我为你提供了一些建议

1)var elLength = form1.elements.length;  //尝试使用此 - &gt;的document.getElementById( “form1中”);有时它与不同的网络浏览器不兼容

2)你为什么不使用jquery?如下所示 How to use jQuery to call an ASP.NET web service?

答案 2 :(得分:0)

使用以下脚本而不是之前的脚本运行页面就像魅力一样。不知道为什么之前没有用。

<script type="text/javascript">
    function SendForm() {
        var email = $get("txtEmail").value;
        var elLength = form1.elements.length;
        for (i=0;i< elLength;i++) {
            var type = form1.elements[i].type;
            if (type == "checkbox" && form1.elements[i].checked) {
                alert("true!");
            }
            else {
                alert("false!");
            }
        }
        PageMethods.SendForm(email, chk, OnSucceeded, OnFailed);
    }
</script>