简体   繁体   English

HTML/JS - 使用来自 HTML 字段元素的值填充“确认”弹出窗口

[英]HTML/JS - Populate a “confirm” popup with a value from an HTML field element

I have the following form fields to collect basic input from the user (a dollar amount).我有以下表单字段来收集用户的基本输入(金额)。

        <tr><td><span class="qText" name="settlement">1. Amount:</span></td></tr> 
        <td><input type="hidden" class="qAns" name="settlement_t1_a">
            <input type="text" class="qAns" id="recovery_val" name="settlement_t1" oninput="formatValue('1');" required></td>

I'm trying to display a confirmation popup when the user clicks the submit button to say "verify amount is correct", and show the amount they entered in the above field "settlement_t1".当用户单击提交按钮说“验证金额是否正确”时,我试图显示一个确认弹出窗口,并显示他们在上述字段“settlement_t1”中输入的金额。

What am I doing incorrectly here?我在这里做错了什么?

<div class="submitDiv"><input type="submit" onclick="return confirm('Please confirm recovery amount is correct: $' + document.getElementsByName("settlement_t1")[0].value;" value="Submit Form"></div>

Everything works fine until I try to insert the element.一切正常,直到我尝试插入元素。 As in, I can get it to prompt "Please confirm recovery amount is correct: $", but without the amount.如中,我可以让它提示“请确认恢复金额是正确的:$”,但没有金额。

 function doConfirm() { return confirm('Please confirm recovery amount is correct: $' + document.getElementsByName("settlement_t1")[0].value); } function formatValue(value) { return value; }
 <form method="post"> <input type="hidden" class="qAns" name="settlement_t1_a" /> <input type="text" class="qAns" id="recovery_val" name="settlement_t1" oninput="formatValue('1');" required /> <div class="submitDiv"><input type="submit" onclick="return doConfirm()" value="Submit Form" /></div> </form>

A big part of the problem is that you are using inline event attributes, which is a 25+ year old legacy approach to event handling that just needs to die but doesn't because people see other people using it and they just copy it.问题的很大一部分是您正在使用内联事件属性,这是一种已有 25 年历史的事件处理遗留方法,它只需要死,但不会因为人们看到其他人在使用它并且他们只是复制它。 Instead, separate your JavaScript from your HTML and set up events with .addEventListener() .相反,将 JavaScript 与 HTML 分开,并使用 .addEventListener .addEventListener()设置事件。

You are also adding a click event handler to a submit button, which triggers the submit event of the form when you should be adding your code to the submit event of the form instead.您还将click事件处理程序添加到submit按钮,当您应该将代码添加submit formsubmit事件时,它会触发form的提交事件。

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

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