使用香草JavaScript在Bootstrap模型中显示来自输入和选择字段的信息

时间:2018-12-20 17:33:52

标签: javascript html twitter-bootstrap

我有多个输入字段和2个选择字段,并且给了它们所有的inputValue类。当用户填写表格时,我想以引导模式显示其所有信息。

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

目前我正在收到一个Uncaught 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>

1 个答案:

答案 0 :(得分:1)

您在这里有一些错误:

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

  2. 您正在通过为每个input元素提供一个inputValue类来选择输入,但是您(我误认为是)将一个inputValue类添加到标签上,当您尝试获取其值,它将返回未定义。

  3. modalText.text()文本不是函数,而是必须通过赋值运算符=

  4. 赋值的属性。
  5. 您还有一个错字而不是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>