2011-12-23 153 views
0

這讓我拉我的頭髮。網站上的按鈕有onclick=method(),它不會調用該方法。該方法應該抓住所有的複選框,檢查他們的檢查狀態,並用true/false填充chk[]數組。然後WebMethod接受該數組,將其分解爲三個較小的數組並對組合執行檢查。就我所知,按鈕從不會調用方法開始。Javascript函數未被onclick調用

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."); 
     } 
    } 
} 

編輯!!: 運行頁面下面的腳本,而不是以前的劇本就像一個魅力。不知道爲什麼以前沒有工作。

<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> 
+0

如果可能,請分享您在瀏覽器中看到的aspx頁面的html輸出。 – Diode 2011-12-23 03:53:43

回答

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> 
0

,而不是調用像

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

你的函數嘗試調用它像這樣

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

我有一對夫婦的建議對您

1)VAR elLength = Form1中.elements.length; //嘗試使用this - > document.getElementByID(「form1」);有時它並不是用不同的網絡瀏覽器

2)你爲什麼不使用jquery?像下面這一個 How to use jQuery to call an ASP.NET web service?