将文本输入乘以选定的单选按钮

你好,我是 javascript 新手,遇到了一些麻烦。


我需要我的函数来获取用户输入“recserv”并将其乘以所选单选按钮的值,以及在值更改或单选按钮更改时进行更新。


更改单选按钮似乎有效,但是当更改“recserv”值时出现错误。


感谢您的任何帮助!


<script>

function yeartot(service) {

    var recserv = parseFloat(document.getElementById('recserv').value);

    document.getElementById("result").value = service*recserv;

}

</script>

<body>


<p>Select the frequency</p>

  <input onclick="yeartot(this.value)" type="radio" name="service" value="11">Monthly<br>

  <input onclick="yeartot(this.value)" type="radio" name="service" value="6" checked> Bi-Monthly<br><br>


 Recurring Service Amount <input onchange="yeartot()" id="recserv" value=0><br/><br/>


  Total for year <input type="text" id="result">


慕森卡
浏览 108回答 3
3回答

潇湘沐

您得到的原因NaN是因为在#recserv元素的内联 JS 中,您在调用函数时没有将任何值传递到函数中。因此,您乘以undefined它就会得到一个NaN值。解决问题的快速方法就是让方法本身检索输入的检查值,并且无需向其传递任何参数。然而,这是一个快速修复,我永远不会建议使用内联 JS:检查下一个示例以获得正确的解决方案。function yeartot() {&nbsp; var recserv = +document.getElementById('recserv').value;&nbsp; var checkedService = +document.querySelector('input[name="service"]:checked').value;&nbsp; document.getElementById("result").value = checkedService * recserv;}<p>Select the frequency</p><input onclick="yeartot()" type="radio" name="service" value="11">Monthly<br><input onclick="yeartot()" type="radio" name="service" value="6" checked> Bi-Monthly<br><br> Recurring Service Amount <input onchange="yeartot()" id="recserv" value=0><br/><br/> Total for year <input type="text" id="result">建议的解决方案:我建议您:用于.addEventListener监听输入元素的更改。您可以使用它document.querySelectorAll([selector])来选择要将oninput事件侦听器绑定到的输入。回调将简单地调用yeartot()在运行时调用yeartot(),以便在加载文档时获得计算值。function yeartot() {&nbsp; var recserv = +document.getElementById('recserv').value;&nbsp; var checkedService = +document.querySelector('input[name="service"]:checked').value;&nbsp; document.getElementById("result").value = checkedService * recserv;}document.querySelectorAll('#recserv, input[name="service"]').forEach(function(el) {&nbsp; el.addEventListener('change', function() {&nbsp; &nbsp; yeartot();&nbsp; });});// Also, might want to run the first round of computation onloadyeartot();<p>Select the frequency</p><input type="radio" name="service" value="11">Monthly<br><input type="radio" name="service" value="6" checked> Bi-Monthly<br><br> Recurring Service Amount <input id="recserv" value=0><br/><br/> Total for year <input type="text" id="result">

皈依舞

简直就是这个……document.getElementById('my-form').addEventListener('input', function () {&nbsp; this.result.textContent = parseFloat(this.recserv.value) * parseInt(this.service.value)})<form id="my-form" onsubmit="return false">&nbsp; <p>Select the frequency</p>&nbsp; <input type="radio" name="service" value="11">Monthly&nbsp; <br>&nbsp; <input type="radio" name="service" value="6" checked> Bi-Monthly&nbsp; <br><br>&nbsp; Recurring Service Amount <input name="recserv" value="0">&nbsp; <br /><br />&nbsp; Total for year <output name="result"></output></form>-- 如果每个条目(或输出)都有名称,则添加表单会使事情变得更容易。-- 使用表单元素,不需要使用任何ID,也不需要查看哪个单选按钮被选中,直接取选择的值 --并且不使用change事件而是使用input事件

宝慕林4294392

const setup = () => {&nbsp; &nbsp; const serviceInputs = document.querySelectorAll('input[name="service"]');&nbsp; serviceInputs.forEach(e => e.addEventListener('click', yeartot));&nbsp; &nbsp; const recserv = document.querySelector('#recserv');&nbsp; recserv.addEventListener('change', yeartot);}const yeartot = (service) => {&nbsp; &nbsp; const checkedInput = document.querySelector('input:checked');&nbsp; &nbsp; const recserv = document.querySelector('#recserv');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const serviceNumber = parseFloat(checkedInput.value, 10);&nbsp; &nbsp; const recservNumber = parseFloat(recserv.value, 10);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const result = document.querySelector('#result');&nbsp; &nbsp; result.value = serviceNumber * recservNumber;}//loadwindow.addEventListener('load', setup);<body>&nbsp; <p>Select the frequency</p>&nbsp; <input type="radio" name="service" value="11">Monthly<br>&nbsp; <input type="radio" name="service" value="6" checked> Bi-Monthly<br><br>&nbsp;Recurring Service Amount <input id="recserv" value="0"><br/><br/>&nbsp; Total for year <input type="text" id="result">&nbsp;&nbsp;</body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5