只有最后一行入到数据库中,而不是所有行?Java脚本

Only last row is being inserted into database instead of all the rows? Javascript

提问人:genericdude76 提问时间:3/24/2023 更新时间:3/24/2023 访问量:80

问:

$(document).on("click", "#devSaveBtn", function () {
  var answer = window.confirm("Save changes");
  if (answer) {
    // ! -------Gets the input from record row--------

    // * Table variable
    var table = document.getElementById("steptable");
    // ! Since there is an extra th element in the table (the headers) the loop has to start from 1 and not zero
    for (var i = 1, row; (row = table.rows[i]); i++) {
      // * iterate through rows
      // * rows would be accessed using the "row" variable assigned in the for loop
      ObjType = table.rows[i].cells[2].querySelector(".vcheck").value;
      Name = table.rows[i].cells[3].querySelector(".vcheck").value;
      Business = table.rows[i].cells[4].querySelector(".vcheck").value;
      Project = table.rows[i].cells[5].querySelector(".vcheck").value;
      Deployer_Candidate =
        table.rows[i].cells[6].querySelector(".vcheck").value;
      Description = table.rows[i].cells[7].querySelector(".vcheck").value;

      // * Condition to prevent the other rows from being inserted with the sql insert statement
      if (Name === "") {
        continue;
      }

      console.log(row);
      console.log("ObjType = " + ObjType);
      console.log("Name = " + Name);
      console.log("Business = " + Business);
      console.log("Project = " + Project);
      console.log("Deployer_Candidate = " + Deployer_Candidate);
      console.log("Description = " + Description);
    }

    // * Loop that will run the sql statement for each row in the table
    for (var i = 0; i < selectTkt.length; i++) {
      if (testSelect != "NEW") {
        console.log("response one");
      }

      // * conditional for new tickets being created
      if (testSelect === "NEW") {
        //prettier-ignore
        callDB(
        "INSERT INTO DB(SEQ_NUM, OBJ_TYPE, OBJ_NAME, DEVELOPER_NAME, CHANGE_NUM, DEPLOYER_BUSINESS, DEPLOY_DATETIME, DEPLOYER_PROJECT, DEPLOYER_CANDIDATE , STATUS, TICKET_NUM, CHANGE_ENGINEER, DESCRIPTION) VALUES ('"+ Math.floor(Math.random() * 10000) + 1 +"','"+ ObjType +"','" + Name + "','asalem1', '.','" + Business + "', '.', '" + Project + "', '"+ Deployer_Candidate +"', 'existing', 'test', 'test', '"+ Description +"')"
      );
        console.log("response two");
      } else {
      }

      // * condition to break the loop so it does not repeat with the clone rows you can't see
      if ((i = selectTkt.length)) {
        console.log("Brake");
        break;
      }
    }
    // * conditional for updating existing tickets

    // * Forces page reload
    // location.reload();
  }
  // * else display service message
  else {
    console.log("Service Cancel");
  }
});
                    <!-- * Main Record Table *-->
                    <div id="steptablecontainer">
                        <div id="steptable_wrapper" class="dataTables_wrapper no-footer">
                            <table id="steptable" class="compact cell-border no-footer dataTable row-border"
                                role="grid">
                                <thead id="recordTableHead">
                                    <tr role="row">
                                        <!-- * Names For Each Container *-->
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label=""
                                            style="width: 25px; max-width: 25px;"></th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label=""
                                            style="width: 50px; background-image: none; max-width: 25px;">Record</th>
                                        <th class="componentellipsis sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Component"
                                            style="width: 200px; max-width: 50px;">Type</th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Type"
                                            style="width: 125px;">Name</th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Test"
                                            style="width: 100px; max-width: 50px;">Business</th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Dev"
                                            style="width: 200px; max-width: 50px;">Project</th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Type"
                                            style="width: 150px; max-width: 125px;">Deployer Candidate</th>
                                        <th class="noncomponentswidth sorting_disabled" tabindex="0"
                                            aria-controls="steptable" rowspan="1" colspan="1" aria-label="Type"
                                            style="width: 400px; max-width: 200px; overflow: hidden;">Description
                                        </th>
                                    </tr>
                                </thead>
                                <!-- * Sample Record (Cloned then changed) *-->

                                <tbody id="recordRow0" style="display: none; border: 10px;" class="recordRow">
                                    <tr role="row" class="" data-true-step="1">
                                        <td class="noncomponentswidth">
                                            <button class="readyonlyhidden" id="removeRecordBtn0" title="Remove Record"
                                                value="0">
                                                <span><i style="font-size:18px;" class="fa fa-minus-circle"></i></span>
                                            </button>
                                        </td>


                                        <!-- * Record intializing *-->
                                        <td class="noncomponentswidth" id="recordCount">1</td>

                                        <!-- * Drop down for the obj_type *-->
                                        <div id="container">
                                            <td class="componentellipsis" id="recordTypeWrapper">
                                                <select id="OBJ_TYPE" class="vcheck">
                                                    <option value="TP">TP</option>
                                                    <option value="Workflow">Workflow</option>
                                                    <option value="Package">Package</option>
                                                </select>
                                            </td>
                                        </div>


                                        <!-- * Container for Obj Name *-->
                                        <td class="noncomponentswidth" id="recordObjName">
                                            <p>
                                                <!-- * Input for Obj Name *-->
                                                <input type="text" id="OBJ_NAME" name="Object Name" placeholder=""
                                                    class="vcheck" required minlength="8">
                                            </p>
                                        </td>

                                        <!-- * Deployer Business *-->
                                        <td class="noncomponentswidth" id="recordDevName">
                                            <input type="text" id="DEPLOYER_BUSINESS" name="Business Name"
                                                placeholder="" style="display: none;">
                                            <select id="DEPLOYER_BUSINESS" class="vcheck">
                                                <!-- * Picklist for TP *-->
                                                <option value="APPM">APPM</option>
                                                <option value="Asia">Asia</option>
                                                <option value="CorporateAP">CorporateAP</option>
                                                <option value="CorporateCredit">CorporateCredit</option>
                                                <option value="CorporateEHS">CorporateEHS</option>
                                                <option value="CorporateFinance">CorporateFinance</option>
                                                <option value="CorporateFinancialServices">
                                                    CorporateFinancialServices
                                                </option>
                                                <option value="CorporateHR">CorporateHR</option>
                                                <option value="CorporateIT">CorporateIT</option>
                                                <option value="CorporateLegal">CorporateLegal</option>
                                                <option value="CorporateLogistics">CorporateLogistics</option>
                                                <option value="CorporateTax">CorporateTax</option>
                                                <option value="EMEAPackaging">EMEAPackaging</option>
                                                <option value="EMEAPapers">EMEAPapers</option>
                                                <option value="GCF">GCF</option>
                                                <option value="MillManufacturing">MillManufacturing</option>
                                                <option value="MillSupport">MillSupport</option>
                                                <option value="NAIPGContainer">NAIPGContainer</option>
                                                <option value="NAIPGContainerboard">NAIPGContainerboard</option>
                                                <option value="NAIPGRecycling">NAIPGRecycling</option>
                                                <option value="NAP">NAP</option>
                                                <option value="SouthAmerica">SouthAmerica</option>
                                                <option value="zConnectivity">zConnectivity</option>
                                                <option value="zSylvamo">zSylvamo</option>
                                            </select>

                                        </td>

                                        <!-- * Input for Deployer Project *-->
                                        <td class="noncomponentswidth" id="recordProjectName">
                                            <input type="text" id="DEPLOYER_PROJECT" name="Project Name" class="vcheck"
                                                placeholder="">
                                        </td>


                                        <!-- * Deployer_Candidate *-->
                                        <td class="noncomponentswidth" id="titleDeployerCandidate">
                                            <select id="DEPLOYER_CANDIDATE" class="vcheck">

                                                <!-- * Picklist for TP *-->
                                                <option value="PRODJ (wm53a/b/c 10000)">PRODJ (wm53a/b/c 10000)
                                                </option>
                                                <option value="PRODR (wm53a/b/c 12000)">PRODR (wm53a/b/c 12000)
                                                </option>
                                                <option value="PRODXA (wm54a/b/c 21000)">PRODXA (wm54a/b/c
                                                    21000)
                                                </option>
                                                <option value="PRODXS (wm54a/b/c 20000)">PRODXS (wm54a/b/c
                                                    20000)
                                                </option>
                                                <option value="PRODXE (wm53a/b/c 11000)">PRODXE (wm53a/b/c
                                                    11000)
                                                </option>
                                                <option value="-------------------">-------------------</option>
                                                <option value="PRODAD1 (wm54a/b/c 60000)">PRODAD1 (wm54a/b/c 60000)
                                                </option>
                                                <option value="PRODAD2 (wm54a/b/c 61000)">PRODAD2 (wm54a/b/c 61000)
                                                </option>
                                                <option value="-------------------">-------------------</option>
                                                <option value="MAPGRP1 (wm52a/b/c 40k/41k/42k)">MAPGRP1
                                                    (wm52a/b/c
                                                    40k/41k/42k)</option>
                                            </select>

                                        </td>

                                        <!-- * Input for Description *-->
                                        <td class="noncomponentswidth" id="recordDeployerCandidate" placeholder="">
                                            <input type="text" id="DESCRIPTION" name="Description" placeholder=""
                                                class="vcheck" style="resize: vertical; width: 100%; height: 4.5em;"
                                                </td>
                                    </tr>
                                </tbody>
                            </table>

                            <!-- * Add & Remove buttons *-->
                            <div class="btn-group2">
                                <div id="addRemoveButons" class="dt-buttons" style="visibility:hidden">
                                    <button class="readonlyhidden" tabindex="0" aria-controls="steptable" type="button"
                                        id="recordAddBtn2" title="Add new record" style="display: inline-block;">
                                        <span><i class="fa fa-plus-circle"></i></span>
                                    </button>
                                    <button class="readonlyhidden" tabindex="0" aria-controls="steptable" type="button"
                                        id="recordAddBtn" title="Add new record">
                                        <span><i class="fa fa-plus-square"></i></span>
                                    </button>
                                    <button class="readonlyhidden" tabindex="0" aria-controls="steptable" type="button"
                                        id="recordRemoveBtn" title="Remove last record" style="display: none">
                                        <span><i class="fas fa-trash-alt"></i></span>
                                    </button>

                                </div>
                            </div>
                        </div>
                    </div>

所以我有一个表,用户可以在其中为输入添加多行。问题是无论添加多少行,都只使用最后一行信息。因此,而不是返回例如(第 1 行:1、2、3) (第 2 行:4,5,6)和(第 3:7、8、9 行)例如,所有三行在输入框中都返回 7、8、9。不知道它为什么要这样做,因为它应该循环输入,然后插入到数据库中,然后再次循环。相反,它看起来像是循环遍历输入,直到到达最后一行,并使用最后一行输入插入三次。

JavaScript HTML jQuery 循环

评论

0赞 mykaf 3/24/2023
您正在循环以在行上的循环之后插入。对行的每个循环都会覆盖上一行中的变量。
0赞 genericdude76 3/24/2023
现在这是有道理的。那么我只需要将代码移动到变量行循环中吗?
0赞 mykaf 3/24/2023
或?你也在循环,但你没有告诉我们这里那个变量中有什么。selectTkt
0赞 genericdude76 3/24/2023
selectTkt 只是表的票证 ID 号。好的,我尝试你说的,这似乎主要解决了插入问题,但现在表格永远在迭代和循环。我需要什么条件来阻止这种情况发生?循环应在到达表中的最后一行时结束
0赞 mykaf 3/24/2023
听起来像正在修改,因此它的长度正在增加,从而继续循环。selectTkt

答: 暂无答案