简体   繁体   English

Javascript document.getElementById function 未将复选框值返回到 HTML 表单

[英]Javascript document.getElementById function not returning checkbox value to HTML form

I have been searching for an answer everywhere but just can not find what I need.我一直在到处寻找答案,但找不到我需要的东西。

I have a webpage that has an HTML table on the left column and an HTML form on the right column.我有一个网页,左栏有一个 HTML 表,右栏有一个 HTML 表格。 When I click on a row in the table on the left I want it to display the values in the form on the right.当我单击左侧表格中的一行时,我希望它在右侧的表格中显示值。

I have this working perfectly for the text fields on the form, but not for the two checkboxes.我可以完美地处理表单上的文本字段,但不适用于两个复选框。 My javascript will return either true or false or checked and unchecked to the document.getElementById within the script itself by using alert();我的 javascript 将返回 true 或 false 或选中和未选中到脚本本身中的document.getElementById使用alert(); but I have no idea what it needs to allow the checkboxes to display these values.但我不知道允许复选框显示这些值需要什么。 I thought the document.getElementById would return the values but it seems it does not.我认为document.getElementById会返回值,但似乎没有。

I have tried all kinds of conveluted ways to get this to work but can not seem to get the correct code needed.我已经尝试了各种复杂的方法来让它工作,但似乎无法获得所需的正确代码。

I am new to all this so there is most likely something really simple I am missing.我对这一切都很陌生,所以很可能我缺少一些非常简单的东西。

This is the HTML form code:这是 HTML 表格代码:

<div class="column right">
  <table>
    <tr></tr>
    <tr>
      <div class="lockinv">
        <form autocomplete="off" name="lockform" class="keyassign" action="includes/lockinventory.inc.php"
          method="post">
          <label id="inventory" for="locknum">Lock Number</label>
          <input id="invlocknum" type="text" name="locknum" value="" required>
          <label id="inventory" for="locktype">Lock Type</label>
          <input id="invlocktype" type="text" name="locktype" value="" required>
          <label id="inventory" for="keycode">Key Code</label>
          <input id="invkeycode" type="text" name="keycode" value="" required>

          <label id="inventory" for="lockengraved">Lock Engraved</label>
          <input id="invlockengraved" type="hidden" name="lockengraved" value="0">
          <input id="invlockengraved" type="checkbox" name="lockengraved" value="1">

          <label id="inventory" for="lockmastered">Lock Mastered</label>
          <input id="invlockmastered" type="hidden" name="lockmastered" value="0">
          <input id="invlockmastered" type="checkbox" name="lockmastered" value="1">

          <label id="inventory" for="locknote">Lock Note</label>
          <textarea id="inventorynote" name="locknote" rows="5" cols="60"></textarea>

          <div class="wheel">
            <?php
              if (isset($_GET["error"])) {
                  if($_GET["error"] == "lockexists") {
                      echo "<p>Lock Already In Inventory!</p>";
                  }
                  else if ($_GET["error"] == "lockexistsfailed") {
                      echo "<p>Lock Already In Inventory!</p>";
                  }
              }
            ?>
          </div>

          <input id="bt6" type="submit" name="submit" value="Save">
          <button id="bt6" type="reset" name="button">Cancel</button>
        </form>
      </div>
    </tr>
  </table>
</div>

This is my JavaScript code:这是我的 JavaScript 代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>

<script language="javascript"></script>

<script>
  $(function () {
    $('#lockTable td').click(function () {

      var currentRow = $(this).closest("tr");
      var locknum = currentRow.find("td:eq(0)").text();
      var locktype = currentRow.find("td:eq(1)").text();
      var keycode = currentRow.find("td:eq(2)").text();
      var engraved = currentRow.find("td:eq(3)").find(":checkbox");
      var mastered = currentRow.find("td:eq(4)").find(":checkbox");
      var locknote = currentRow.find("td:eq(5)").text();

      var lockengraved = engraved.prop("checked");
      if (lockengraved === true) {
        $grved = "checked";
      } else {
        $grved = "unchecked";
      }

      var lockmastered = mastered.prop("checked");
      if (lockmastered === true) {
        $msted = "checked";
      } else {
        $msted = "unchecked";
      }

      document.getElementById('invlocknum').value = locknum;
      document.getElementById('invlocktype').value = locktype;
      document.getElementById('invkeycode').value = keycode;
      document.getElementById("invlockengraved").value = $grved;
      document.getElementById('invlockmastered').value = $msted;
      document.getElementById('inventorynote').value = locknote;

      alert(document.getElementById("invlockengraved").value);
      alert(document.getElementById("invlockmastered").value);

    });
  });
</script>
<p id="invlocknum"></p>
<p id="invlocktype"></p>
<p id="invkeycode"></p>
<p id="invlockengraved"></p>
<p id="invlockmastered"></p>
<p id="invlocknote"></p>
    
<p id="info"></p>
<p id="result"></p>

I found the answer to my issue.我找到了我的问题的答案。 I had to modify my if statement in the Javascript to the following.我不得不将 Javascript 中的 if 语句修改为以下内容。 Works the way I want it to.按我想要的方式工作。 Thanks to all that helped.感谢所有帮助。

var lockengraved = engraved.prop("checked");
    if (lockengraved === true) {
      document.getElementById("invlockengraved").checked = lockengraved;
    }else if (lockengraved === false) {
      document.getElementById("invlockengraved").checked = lockengraved;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM