javascript - 使用 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">×</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>
解决方案
你这里有一些错误:
您已经
onclick
在带有功能的按钮上放置了一个,submitText
但 js 中的功能名称是submitSomeText()
input
您通过给每个元素一个类来选择输入,inputValue
但是我错误地假设将一个类放在inputValue
标签上,当您尝试获取其值时,它将返回未定义。modalText.text()
text 不是一个函数,而是一个必须通过赋值运算符赋值的属性=
你也有一个错字,而不是
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">×</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>
推荐阅读
- python-3.x - Pandas - 如果第二个字符串包含第一个字符串,则合并数据帧
- javascript - IOS浏览器中的javascript打印不打印对话框
- javascript - 网站缓存——浏览器缓存网站页面/js,避免用户看到网站更新
- java - 拆分后如何将值添加到同一字符串
- amazon-web-services - 在 S3 存储桶中拒绝具有特定标签值的 DeleteObject
- java - 如何在 AsyncTask 上使用 .getResources()?
- vue.js - vue 选择所有具有由计算方法生成的值的复选框
- java - 如何在 Tomcat 9 上使用 Log4J 配置应用程序日志记录?
- r - R:在 R 中的 VPN 之间切换
- c# - 如何同时模拟 Directory.getFiles 确保生产代码也运行