gpt4 book ai didi

javascript - 使用 vanilla javascript 在 Bootstrap 模式中显示输入信息并选择字段

转载 作者:行者123 更新时间:2023-12-03 00:30:55 26 4
gpt4 key购买 nike

我有多个输入字段和 2 个选择字段,并且我为它们提供了一个 inputValue 类。当用户填写表单时,我想在 Bootstrap 模式中显示他们的所有信息。

我一直在尝试获取输入的值,然后获取索引。类似于 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. 您已放置 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>

关于javascript - 使用 vanilla javascript 在 Bootstrap 模式中显示输入信息并选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com