2013-10-16 24 views
0

我有一個鏈接列表,他們每個人都有href屬性:如何使用href並單擊listener togather?

<a href="./page1.php" class="points">page 1</a> 
<a href="./page2.php" class="points">page 2</a> 
<a href="./page3.php" class="points">page 3</a> 
<a href="./page4.php" class="points">page 4</a> 

和我有類「點」的監聽器:

$(".points").live('click',function(){ 
    id = $(this).attr("id"); 
    $.ajax({ 
     type: "POST", 
     url: 'points.php', 
     data:{id:id}, 
     success: function() 
     { 

     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown) { 
      alert(XMLHttpRequest); 
      alert(textStatus); 
      alert(errorThrown); 
      alert(XMLHttpRequest.responseText); 
     } 
    }); 
}); 

的href是工作,但點擊後不是,當我刪除href屬性時,點擊監聽器完美地工作。這兩個人可以工作嗎?

+3

你嘗試:'$( 「點」)生活( '點擊',功能(E){e.preventDefault();'在你的函數第一線? – Sergio

+6

此外,您可能想使用'.on()'而不是'.live()' – Terry

+1

FYI:'.live'已被棄用了很長時間,並從最新版本的jQuery中刪除。改用'.on'。 –

回答

0

用於Ajax & Href一起工作

嘗試

$(".points").live('click',function(e){ 
    e.preventDefault(); 
    var id = $(this).attr("id"); 
    var url= $(this).attr("href"); 

    $.ajax({ 
    type: "POST", 
    url: 'points.php', 
    data:{id:id}, 
    success: function() 
    { 
    //Do something before going to page 
     window.location.href=url; 

    }, 
    error : function(XMLHttpRequest, textStatus, errorThrown) { 
     alert(XMLHttpRequest); 
     alert(textStatus); 
     alert(errorThrown); 
     alert(XMLHttpRequest.responseText); 
    } 
    }); 
    }); 
+1

你需要'preventDefault()'來運行這個。 – Krishna

2

您需要取消默認行爲,當您離開時,腳本將停止運行。你可以考慮等到Ajax調用完成後,然後通過腳本導航:

$(".points").live('click',function(e){ // <--- Add e parameter 

    e.preventDefault(); // <--- Add this 

    id = $(this).attr("id"); 
    href = $(this).attr("href"); // Link we will navigate to when done 
    $.ajax({ 
     type: "POST", 
     url: 'points.php', 
     data:{id:id}, 
     success: function() 
     { 
      location.href = href; // Do the navigation now that we're done 
     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown) { 
      alert(XMLHttpRequest); 
      alert(textStatus); 
      alert(errorThrown); 
      alert(XMLHttpRequest.responseText); 
     } 
    }); 
}); 
+0

我希望他們工作togather,如果我添加此行,它將只運行JavaScript,但href不會工作(我試試) – Basel

+2

是的,沒有辦法做到這一點。當你離開的時候,腳本將停止在該頁面上運行。 –

+0

@BaselShbeb - 一種可能的解決方案是等待Ajax調用完成,然後離開。我已經更新了我的代碼(未經測試,希望它可以工作) –

2

試試這個:

$(".points").live('click',function(e){ //added e as function argument 
    e.preventDefault(); //added preventDefault(); 
    var id = this.id; // changed here to this.id, and added var (so it will be a local variable) 
    $.ajax({ 
     type: "POST", 
     url: 'points.php', 
     data:{id:id}, 
     success: function() 
     { 

     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown) { 
      alert(XMLHttpRequest); 
      alert(textStatus); 
      alert(errorThrown); 
      alert(XMLHttpRequest.responseText); 
     } 
    }); 
}); 
  • 要了解preventDefault()
  • 注意As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.
+1

+1 - 我喜歡使用'this.id'的建議。最重要的是,可能值得一提的是'id = this.id'創建一個全局變量或覆蓋作用域鏈中的本地變量。這可能是正確的,但更可能是OP代碼中的疏忽。 – Steve

+0

@Steve,非常好的一點。補充說! – Sergio

相關問題