2015-12-03 94 views
-2

HTML形式 -Jquery的的onclick功能不能正常工作

<div class="inner-container login-panel id="login_form""> 
    <h3 class="m_title">SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES</h3> 
    <div id="login_form" class="loginform"> 
    <a href="#" class="create_account" onClick="ppOpen('#register_panel', '280');">CREATE ACCOUNT</a> 
    <input type="text" id="email" name="email" class="inputbox" placeholder="Email"> 
    <input type="password" id="password" name="password" class="inputbox" placeholder="Password"> 
    <button id="loginsubmit" class="btn btn-red">LOG IN</button> 
    <a href="#" class="login_facebook">login with facebook</a> 
    </div> 
    <div class="links"> 
    <a href="#" onClick="ppOpen('#forgot_panel', '350');">FORGOT YOUR USERNAME?</a>/<a href="#" onClick="ppOpen('#forgot_panel', '350');">FORGOT YOUR PASSWORD?</a> 
    </div> 
</div> 

和JavaScript代碼 -

console.log("before"); 
$(document).ready(function(){ 
    console.log("then"); 
    $("#loginsubmit").click(function(){ 
     console.log("success"); 
     e = $("#email").val(); 
     p = $("#pass").val(); 
     if($("#email").val()==""||$("#pass").val()==""){ 
      $("div#ack").html("please enter email and pass"); 
      } 
     else { 
      $.post($("#myForm").attr("action"), 
        //$("#myForm :input").serializeArray(), 
        {emailid: e, password: p}, 
        function(data){ 
         $("div#ack").html(data); 
        }); 
      }  
     $("#myForm").submit(function(){ 
      return false; 
     }); 
    }); 
}); 

在之前的控制檯,然後打印但在點擊#loginsubmit沒有反應。 javascript代碼有問題嗎?請幫我找出答案。

+0

使用大括號,當你使用if ...的多個代碼行else條件。 –

+0

已編輯。仍然沒有迴應。 –

+0

你在控制檯上獲得成功嗎? – sunil

回答

0

您的代碼正在運行,您沒有使用#ack id編寫div元素,如果將添加此元素,您可以看到按鈕單擊上的響應。

0

HTML:<button id="loginsubmit" class="btn btn-red" onclick="logincheck()">LOG IN</button>

的Javascript:

function logincheck(){ 
    console.log("success"); 
    e = $("#email").val(); 
    p = $("#pass").val(); 
    if($("#email").val()==""||$("#pass").val()=="") 
     $("div#ack").html("please enter email and pass"); 
    else 
     $.post($("#myForm").attr("action"), 
       //$("#myForm :input").serializeArray(), 
       {emailid: e, password: p}, 
       function(data){ 
        $("div#ack").html(data); 
       }); 
    $("#myForm").submit(function(){ 
     return false; 
    }); 

} 

現在它工作正常。

-2

敬請使用下面的更新腳本和HTML:

的javascript:

console.log("before"); 
$(document).ready(function(){  
    console.log("then"); 
    $("#loginsubmit").click(function(){ 
     console.log("success"); 
     e = $("#email").val(); 
     p = $("#pass").val(); 
     if($("#email").val()==""||$("#pass").val()==""){ 
      $("div#ack").html("please enter email and pass"); 
     } 
     else { 
      $.post($("#myForm").attr("action"), 
      //$("#myForm :input").serializeArray(), 
      { emailid:e, password:p }, 
       function(data){ 
        $("div#ack").html(data); 
       },'json'); 
      }  
      $("#myForm").submit(function(){ 
       return false; 
      }); 
     }); 
}); 

HTML:

<div class="inner-container login-panel id="login_form"">  
    <h3 class="m_title">SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES</h3> 
    <form action="#" method="post" id="myForm"> 
     <div id="login_form" class="loginform"> 
      <a href="#" class="create_account" onClick="ppOpen('#register_panel', '280');">CREATE ACCOUNT</a> 
      <input type="text" id="email" name="email" class="inputbox" placeholder="Email"> 
      <input type="password" id="password" name="password" class="inputbox" placeholder="Password"> 
      <button id="loginsubmit" class="btn btn-red">LOG IN</button> 
      <a href="#" class="login_facebook">login with facebook</a> 
      <div id="ack"></div> 
     </div> 
    </form> 
    <div class="links"> 
     <a href="#" onClick="ppOpen('#forgot_panel', '350');">FORGOT YOUR USERNAME?</a>/
     <a href="#" onClick="ppOpen('#forgot_panel', '350');">FORGOT YOUR PASSWORD?</a> 
    </div> 
</div>