0

I'm using twitter bootstrap in my application. i gave e.preventDefault for link button in $(document).ready(), but it is not working.

Here is my code:

Master page:

<a id="lnkLogout" href="javascript:void(0);" onclick="PageLogout();"><i class="icon-off"></i>Logout</a>
<asp:Button ID="btnLogout" OnClick="btnLogout_Click" runat="server" class="hidden" />
<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $('#lnkLogout').click(function (e) { e.preventDefault(); });         
    });  
    function PageLogout() {
        $("#btnLogout").click();
    }                
</script>

MasterPage.aspx.cs :

protected void btnLogout_Click(object sender, EventArgs e)
{
    Session.Clear();
    Session.Abandon();
    Session.RemoveAll();
    Response.Redirect("~/SampleWindowCloser.htm");            
}

SampleDefaultPage.aspx :

<div class="row-fluid">
     <div class="form-horizontal">
          <h4 class="header blue bolder smaller">
              Contact</h4>
          <div class="control-group">
              <label for="form-field-email" class="control-label">
                    Email</label>
              <div class="controls">
                   <input type="text" style="width:200px;" data-val="true" 
                           data-val-required="Mobile Number is required."  
                           id="txtEmail">
                   <div class="space-3">
                   </div>
                   <span data-valmsg-replace="true" data-valmsg-for="txtEmail" 
                         class="field-validation-valid text-warning red"></span>
              </div>
          </div>
          <div class="control-group">
               <label for="form-field-website" class="control-label">
                      Phone Number</label>
               <div class="controls">
               <input type="text" style="width:200px;" 
                      id="txtPhno">                             
         </div>
   </div>   
</div>

I have given txtEmail as mandatory field. if i left that field blank and click on Logout button in Master page, it says txtEmail is required. i dint do any validation in Logout onclick event. I have given preventDefault for Logout link. But still facing an issue. I should be able to logout even though txtEmail is left blank.

Thank you all in advance for your response.

4

2 回答 2

0

尝试这个,

$(document).ready(function () {
    $('#lnkLogout').click(function (e) { 
       e.preventDefault(); 
       // code for logout from application
    });         
}); 

HTML

 <a id="lnkLogout" href="javascript:void(0);"><i class="icon-off"></i>Logout</a>

更新

$(document).ready(function () {
    $('#lnkLogout').click(function (e) { 
       e.preventDefault(); 
       $("#btnLogout").click();
    });         
});
于 2013-11-11T04:14:13.173 回答
0

你试过return false吗?

  <a id="lnkLogout" href="javascript:void(0);" onclick="PageLogout(); return false;">

或者将事件处理程序与PageLogout()内部Click()一起e.preventDefault()放入:

<a id="lnkLogout" href="javascript:void(0);"><i class="icon-off">
                        </i>Logout</a>

<script type="text/javascript" language="javascript">
  $(document).ready(function () {
  $('#lnkLogout').click(
   function PageLogout(e) {
         // more code here
         e.preventDefault(); });         
    });  
</script>

参见JSFiddle 演示

于 2013-11-11T03:54:00.673 回答