首页 > 解决方案 > 使用 vanilla javascript 在引导模型中显示来自输入和选择字段的信息

问题描述

我有多个输入字段和 2 个选择字段,我已经给了它们一个 inputValue 类。当用户填写表单时,我想在引导模式中显示他们的所有信息。

我一直在尝试获取输入的值,然后获取索引。类似于这里的做法

目前我得到一个未捕获的 ReferenceError 错误,如下所示,这很奇怪,我不知道为什么。

我希望这是有道理的。

const modalText = document.gteElementById('modalText')
const inputValue = document.getElementsByClassName("inputValue");

function submitSomeText() {
  modalText.text("Please confirm this info: " + "Company: " + inputValue[0].value +
  "Address Line One " + inputValue[1].value + "City/Town: " + inputValue[2].value  + "Post Code: " + inputValue[3].value + "COMMODITIES " + inputValue[4].value + "Storage Type" + inputValue[5].value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="control-group ">
  <label class="control-label">Company</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey" name="CollectionCompany" type="text" placeholder="Company" class=" input-border ">
    <p class="help-block"></p>
  </div>
</div>
<div class="control-group ">
  <label class="control-label inputValue">Address Line One</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="Street address, P.O. Box, Company Name..." class=" input-border">
  </div>
</div>
<div class="control-group ">
  <label class="control-label">City/Town</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="City/Town" class=" input-border">
  </div>
</div>
<div class="control-group ">
  <label class="control-label">Postal Code</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="Postal Code" class=" input-border">
  </div>
</div>

<div class="col-md-6">
  <p class="mb-1 text-uppercase left mb-0">Commodities</p>
  <select name="Address" class="classic text-uppercase center w-100 mb-4 inputValue" name="PalletType" id="grey">
    <option selected >Food</option>
    <option>Plants</option>
    <option>Electronics</option>
    <option>Other</option>
  </select>
</div>

<div class="col-md-6">
  <p class="mb-1 text-uppercase left mb-0">Storage Type</p>
  <select name="Address" class="classic text-uppercase center w-100 mb-4 inputValue" name="PalletType" id="grey">
    <option selected >Ambient</option>
    <option>Refrigerated</option>
  </select>
</div>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong" onClick="submitText()">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id="modalText">
   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary">Confirm</button>
      </div>
    </div>
  </div>
</div>

标签: javascripthtmltwitter-bootstrap

解决方案


你这里有一些错误:

  1. 您已经onclick在带有功能的按钮上放置了一个,submitText但 js 中的功能名称是submitSomeText()

  2. input您通过给每个元素一个类来选择输入,inputValue但是我错误地假设将一个类放在inputValue标签上,当您尝试获取其值时,它将返回未定义。

  3. modalText.text()text 不是一个函数,而是一个必须通过赋值运算符赋值的属性=

  4. 你也有一个错字,而不是gteElementById应该getElementById注意“get”这个词

const modalText = document.getElementById('modalText')
const inputValue = document.getElementsByClassName("inputValue");

function submitSomeText() {

  modalText.text = "Please confirm this info: " + "Company: " + inputValue[0].value +
    "Address Line One " + inputValue[1].value + "City/Town: " + inputValue[2].value + "Post Code: " + inputValue[3].value + "COMMODITIES " + inputValue[4].value + "Storage Type" + inputValue[5].value;
  console.log(modalText.text)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

<div class="control-group ">
  <label class="control-label">Company</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey" name="CollectionCompany" type="text" placeholder="Company" class=" input-border ">
    <p class="help-block"></p>
  </div>
</div>
<div class="control-group ">
<!-- NOTE you added inputValue class to a label and trying to get its value will gives you undefined-->
  <label class="control-label">Address Line One</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="Street address, P.O. Box, Company Name..." class=" input-border">
  </div>
</div>
<div class="control-group ">
  <label class="control-label">City/Town</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="City/Town" class=" input-border">
  </div>
</div>
<div class="control-group ">
  <label class="control-label">Postal Code</label>
  <div class="controls">
    <input class="input-border collection-input inputValue" id="grey formGroupExampleInput" name="CollectionAddress" type="text" placeholder="Postal Code" class=" input-border">
  </div>
</div>

<div class="col-md-6">
  <p class="mb-1 text-uppercase left mb-0">Commodities</p>
  <select name="Address" class="classic text-uppercase center w-100 mb-4 inputValue" name="PalletType" id="grey">
    <option selected>Food</option>
    <option>Plants</option>
    <option>Electronics</option>
    <option>Other</option>
  </select>
</div>

<div class="col-md-6">
  <p class="mb-1 text-uppercase left mb-0">Storage Type</p>
  <select name="Address" class="classic text-uppercase center w-100 mb-4 inputValue" name="PalletType" id="grey">
    <option selected>Ambient</option>
    <option>Refrigerated</option>
  </select>
</div>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong" onClick="submitSomeText()">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id="modalText">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary">Confirm</button>
      </div>
    </div>
  </div>
</div>


推荐阅读