2009-12-20 45 views
2

我想不通這是爲什麼提醒過我幾次每次更改...爲什麼jQuery的選擇負載循環

$(function(){ 
    $("#shift_name_<?php print $shift_id;?>").change(function() { 
     $(".alertmessage").load("messages.php?eid="+$("#shift_name_<?php print $shift_id;?>").val(), function(data<?php print $shift_id;?>) { 
      if(data<?php print $shift_id;?>!=''){ 
       alert(data<?php print $shift_id;?>); 
      } 
     }); 
    }); 
    }); 

基本上,但是也有一些遍歷並給出了具體的IDS基於幾在一個PHP變量。然而,當我改變一個它會提醒我x次,其中x是頁面上存在的數量...

回答

2

你有多個「alertmessage」div嗎?如果是這樣,你不僅僅是加載其中的一個。我個人不會這樣做。相反,嘗試:

<select name="..." id="shift13" class="alert"> 
    ... 
</select> 
<div id="alert13" class="alertmessage shift13"></div> 

有:

$(function() { 
    $("select.alert").change(function() { 
    $("div.alertmessage." + this.id).load("messages.php", 
     {id: this.id}, function() { 
     alert(this.id); // alert13 
    }); 
    }); 
}); 

是(恕我直言)不是通過循環和重複創建事件處理大量清潔的解決方案。基本上,編碼元素中所需的信息。在上面,組合框的ID提供了AJAX調用的ID並將其鏈接到相關的DIV加載到。