开发者

jquery .change() $ajax() javascript variable issue

开发者 https://www.devze.com 2023-03-18 08:40 出处:网络
I\'m really not sure where the problem is, but the first two <select>+event pairs work, but the third doesn\'t, and it\'s almost exactly the same as the second.

I'm really not sure where the problem is, but the first two <select>+event pairs work, but the third doesn't, and it's almost exactly the same as the second.

The variable "firmName" resolves to a single-word string and if I manually replace the variable with its value, the function works (but it is triggered onload instead of by the change() it's binded to): 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",
            }),开发者_JAVA百科
        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>


Running the code in Chrome, I get Uncaught ReferenceError: clientName is not defined when changing the client-field. Your clientName is not defined as the code stands.

When changing the code as

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

and it runs just fine for me

0

精彩评论

暂无评论...
验证码 换一张
取 消