繁体   English   中英

提交按钮在我的代码中无法正常工作

[英]Submit button not working properly in my code

下面我有一个带有字段的表格。 我尝试使用 JavaScript 打印从字段输入的数据,我可以在 div 中使用 id result获得结果,但是当我在表单中添加相同的 div 时,由于某种原因,从该字段输入的结果没有出现,我不明白. 我在这里错过了什么吗?

ps 我想保存在表单中输入的数据,因为我想打印它

编辑:我尝试了下面的代码并没有解决问题,因为大多数关于 JS 页面刷新的答案

 userNameInput = function(event) { event.preventDefault(); event.stopPropagation(); var AmountPaid = document.getElementById('AmountPaid').value; var threeMonthGoal = document.getElementById('threeMonthGoal').value; var result = document.getElementById('result'); if (AmountPaid.length < 3) { result.textContent = 'Name must contain at least 3 characters'; //alert('Username must contain at least 3 characters'); } else { result.textContent = AmountPaid } } submitButton.addEventListener('click', userNameInput, false);
 <label>المبلغ المدفوع</label> <input id="AmountPaid" type="text"></input> <label>3 Month Goal</label> <input id="threeMonthGoal" type="text"></input> <button id="submitButton" type="submit">Print Name</button> <p id='result'></p> //here it appears </div> <div class="container"> <div id="printMe" class="row" style="padding: 15px; font-family: 'Cairo', sans-serif !important; "> <div> <div> <div class="row bodyPrint" style="box-shadow: #4b4746 0px 0px 0px 1000px inset !important"> <div class="col" style=" position: relative; margin-top: 3rem !important; right: -7px;"> <h6 style="color:white;"> 0780 553 1245</h6> <h6 style="color:white;">0780 456 3215 </h6> </div> <div class="col mt-2"> <img src="newLOgo-white.png" alt="Company logo" style=" margin-bottom: 15px; max-width: 296px "></div> <div class="col " style="position: relative; margin-top: 3rem!important; left: 135px;"> <h6 style="color:white;"> شركة فانتاج العقارية للبيع والبناء</h6> <h6 style="color:white;">Vantage Real Estate </h6> </div> </div> </div> <br> <div class="table-responsive" style="overflow-x: clip !important; padding: 25px;"> <div class="row" style="position: relative; margin-top: -38px; margin-bottom: -13px;"> <div class="col" sm="4 mt-2 "> <button style=" border: 1px solid lightgrey; border-radius: 0px; background: transparent; height: 39px;"> No: </button> </div> <div class="col" sm="4 mt-2 "> </div> <div class="col" sm="4 mt-2 "> <h4 style="color:white!important; box-shadow: #906248 0px 0px 0px 1000px inset !important; padding:5px; text-align:center;"> وصل قبض</h4> </div> <div class="col" sm="4 mt-2 "> </div> <div class="col" sm="4 mt-2 "> <button style=" font-weight: 900; border-radius: 0px; background: transparent; height: 39px; border: .5 px solid grey; position: relative; border: 1px solid lightgrey; left: 109px;"> $ : المبلغ </button> </div> <p id='result'></p> // ican't see result here </div> </div> <div class="row"> <b-button style=" border-radius: 0px; border-radius: 0px; background: transparent; height: 39px; position: relative; left: 15px; bottom: 7px; "> </b-button> </div> <div class="row"> <div> <p id='result'></p> </div> <div class="col-10"> <h5 style="border:.5px solid lightgray; padding:5px; text-align: end;"> مبلغ وقدره </h5> </div> <div class="col" style=" margin: inherit;"> <h5 style="margin-top: 9px;">: استلمــــت من السيــــد</h5> </div> </div> <h5 style=" text-align: end;">العـــــــــــــــــــــــــــــــــائد للشقـــــــــــــــــــــــــة المـــــــــــــــــبينة مواصــــــــــــــفاتها أدنـــــــــــــــاه و فـــــــــــــي التـــــــــــــــــــــــــــــــاريخ اعــــــــــــــــــلاه</h5> <div class="row"> <table class="table table-bordered"> <thead> <tr class="tr-class"> <th> &nbsp;الاتجاه</th> <th>المساحة</th> <th>رقم الشقة</th> <th>الطابق</th> <th> رقم البناية</th> </tr> </thead> <tbody> <tr> <td scope="row"></td> <td></td> <td></td> <td class="mt-3"> </td> <td></td> </tr> </tbody> </table> </div> </div> <div>

我建议您包装在一个表单中并使用提交事件

您可以将 event.preventDefault() 移动到 if 以仅在 AmountPaid > 3 个字符时提交,但为什么不使用数字?

什么名字? 请将问题更新为最小的可重现示例

 const result = document.getElementById('result'); const userNameInput = function(event) { event.preventDefault(); let AmountPaid = +document.getElementById('AmountPaid').value; let threeMonthGoal = document.getElementById('threeMonthGoal').value; result.textContent = +AmountPaid < 100 ? 'Amount must contain at least 3 characters' : AmountPaid } document.getElementById("goalForm").addEventListener('submit', userNameInput);
 <form id="goalForm"> <div> <label>المبلغ المدفوع</label> <input id="AmountPaid" type="text" /> <label>3 Month Goal</label> <input id="threeMonthGoal" type="text" /> <button id="submitButton" type="submit">Print Amount</button> <p id='result'></p> </div> </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM