2011-07-10 107 views
0

我真的不知道問題出在哪裏,但前兩個<select> + event對工作,但第三個不是,它幾乎是完全一樣的第二。

變量「firmName」解析爲一個單字串,如果我手動與它的值替換變量,函數工作(但它是通過改變(onload事件觸發代替),它綁定到):http://www.frende.me/invoiceBuilder.php

<select id="firm" name="firm"></select> 
<select id="client" name="client"></select> 
<select id="project" name="project"></select> 
<select id="task" name="task"></select> 
<p id="results"></p> 
<script> 
window.onload = (function(){ 
    $.ajax({ 
     /* populates the first <select> with <options> */ 
    }); 
}); 

$("#firm").change(function() { 
    var firmName = ""; 
    $("#firm option:selected").each(function() { 
     firmName += $(this).text() + " "; 
    }); 
    $.ajax({ 
     url: '_resources/db_clientworklog_selectField.php', 
     type: "POST", 
     data: ({ 
      table: firmName, 
      column: "client", 
      }), 
     success: function(data){ 
      $("#client").html(data); 
     } 
    }); 
}) 
.change(); 

$("#client").change(function() { 
    var firmName = $("#firm").val(); 
    $("#results").text(firmName); 
    $("#client option:selected").each(function() { 
     clientName += $(this).text() + " "; 
    }); 
    $.ajax({ 
     url: '_resources/db_clientworklog_selectField.php', 
     type: "POST", 
     data: ({ 
      table: firmName, /* if i replace "firmName" with one of the table's names, like "frende", it works (tho it populates immediately onload) */ 
      column: "project", 
      }), 
     success: function(data){ 
      $("#project").html(data); 
     } 
    }); 
}) 
.change(); 
</script> 
+0

這可能不是問題,但你應該拿出逗號在「列的結尾:「客戶端「,」和「列:」項目「,」行,因爲他們在數組的末尾。 IE可能是一個真正的問題。 –

+0

你確定firmName是不是未定義?我會在$(「#firm」)。val()之後向第三個更改綁定中發出警報,以確保它實際上獲取了值。 –

+0

@bob baddeley:謝謝指出:) – jacob

回答

2

運行在Chrome的代碼,我得到 Uncaught ReferenceError: clientName is not defined 改變用戶端場時。您的客戶名稱未定義爲代碼。

當改變代碼

$("#client").change(function() { 
    var clientName = "??"; // define the variable 
    var firmName = $("#firm").val(); 
    $("#results").text(firmName); 
    $("#client option:selected").each(function() { 
     clientName += $(this).text() + " "; 
    }); 
.... 

,它運行得很好,我

+0

您的觸發器同時使用clientName和firmName - 不確定您打算在ajax調用中實際發送哪一個。 – Soren

+0

啊!謝謝!我使用php很多,所以我忘了JavaScript,你必須先定義一個變量,然後才能使用它。 – jacob

+0

btw我實際上同時發送firmName和clientName,因爲firmName也是mysql數據庫中表的名稱,那麼clientName也是表firmName的第一列的名稱。 – jacob