2012-09-26 36 views
0

我對Jquery來說很新,需要幫助,試圖讓一些事件在同一頁面上運行。我有兩個部分的事件,一個是.get,它將數據傳遞給文件,然後顯示檢索到的數據,這發生在頁面加載。第二部分是點擊單擊按鈕時發送和接收數據的點擊事件。在一個頁面上的多個Jquery事件

這些事件都可以根據需要進行工作,但是如果我在一個頁面上有兩個部分,點擊事件就不起作用。下面是我有的代碼,我希望有人知道如何讓他們全部工作?由於

<script type="text/javascript"> 
    $.get("/layout/standard/check.php", { url: "domain"}, function(data){ 
     $("#content-area").html(data) 
    }); 

    $(".discuss").click(function() { 
     $.post('/layout/standard/report-action.php', {form: "discuss"},function(data){ 
     $(".message").html(data); 
     $(".message").show("slow"); 
    }); 
    }); 
    $(".request").click(function() { 
     $.post('/layout/standard/report-action.php', {form: "request"},function(data){ 
     $(".message").html(data); 
     $(".message").show("slow"); 
    }); 
    }); 
    $(".report").click(function() { 
     $.post('/layout/standard/report-action.php', {form: "report"},function(data){ 
     $(".message").html(data); 
     $(".message").show("slow"); 
    }); 
    }); 
</script> 

我相信我已經找到了,爲什麼它不工作,的onclick鏈接由GET事件放在頁面上。看起來,如果按鈕已經在頁面上,按鈕就可以工作,但如果按照這種方式插入按鈕則不會。

這沒有意義,我作爲按鈕仍然充當沒有問題鏈接...

+0

你有沒有在控制檯中的任何錯誤裏面? –

+0

我有一個控制檯錯誤,但我不知道它是否相關:未捕獲TypeError:對象[對象對象]沒有方法'on' – Paul

+0

恐怕沒有工作。 – Paul

回答

1

嘗試代表團..並把你的代碼的document.ready功能

$(function() { 
    $.get("/layout/standard/check.php", { 
     url: "domain" 
    }, function(data) { 
     $("#content-area").html(data) 
    }); 

    $("body").on('click','.discuss,.request,.report',function() { 
     $.post('/layout/standard/report-action.php', { 
      form: $(this).attr('class') //this is assuming you only have 1 class per element 
     }, function(data) { 
      $(".message").html(data); 
      $(".message").show("slow"); 
     }); 
    }); 
});​ 
0

您可能需要委派的事件,如果它們連接到動態創建的內容。嘗試更換:

$(".discuss").click(function() { 

有了:

$('body').on('click', '.discuss', function() { 

並繼續爲所有事件。只要它始終出現在您的文檔中,您就可以用另一個選擇器替換'body'選擇器。

+0

感謝您的建議,它沒有工作。 – Paul

相關問題