2011-12-23 2 views
0

이것은 내 머리카락을 꺼내는 것입니다. 사이트의 버튼은 onclick=method()이며 메소드를 호출하지 않습니다. 메서드는 모든 체크 박스를 잡고 체크 된 상태를 확인하고 chk[] 배열을 true/false로 채우기로되어 있습니다. 그런 다음 WebMethod은 해당 배열을 가져 와서 세 개의 작은 배열로 나누고 조합에 대한 검사를 실행합니다. 지금까지 내가 알 수있는 것처럼, 버튼은 처음부터 메서드를 호출하지 않습니다.자바 스크립트 함수가 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

답변

0

아래에 이와 같은? jQuery를 사용하지 않는 이유

2

) 매력처럼 작동과 호환되지 않습니다 . 이전에 효과가 없었던 이유는 없습니다.

<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 = 유치원 1 .elements.length; // 이것을 사용하려고 시도하십시오 -> document.getElementByID ("form1"); 경우에 따라서는 다른 웹 브라우저 대신 이전 스크립트의 다음 스크립트를 사용하여 페이지를 실행 How to use jQuery to call an ASP.NET web service?

관련 문제