我有多个输入字段和2个选择字段,我给了它们一类InputValue。当用户填写表格时,我想在引导模式中显示所有信息。
我一直在尝试获取输入的值,然后获得索引。类似于此处的工作
目前,我会在下面显示一个未知的参考错误错误,这很奇怪,我无法弄清楚为什么。
我希望这是有道理的。
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>
您在这里有一些错误:
-
您已经在带有函数
submitText
的按钮上放置了一个onclick
,但JS中的功能名称是submitSomeText()
-
您是通过给出每个
input
元素一类inputValue
来选择输入,但是您认为您错误地认为,将inputValue
类放在标签上,当您尝试获取其值时,它将返回Undefined。<<<<<<<<<<<<<<<<<</p> -
modalText.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>