单击提交按钮后删除逗号

在使用以下方法提交到我的数据库之前,我试图从所有输入中删除逗号:


<form method="POST" class="form-horizontal  mb-5" onsubmit="submitForm()">

    <input type="text" name="Salary" class="textInput form-control commanumber">

    <input type="text" name="Bonus" class="textInput form-control commanumber">

    <input type="text" name="Overtime" class="textInput form-control commanumber">

        ....

</form>

function submitForm() {

    var input = $('.commanumber');

    input.val(input.val().replace(/,/g, ''));

}


我设法删除了逗号并将其存储在我的数据库中,但问题是它将所有输入转换为相同的值。现在我所有的输入都有薪水值。


我可以知道如何从我的所有.commanumber输入中正确删除逗号并将其存储在数据库中吗?


温温酱
浏览 142回答 3
3回答

翻过高山走不出你

你应该给你的输入不同的 ID 或类名btn=document.getElementById("btn")btn.addEventListener("click",(e)=>{e.preventDefault() // you should remove this if use this code in your projectsubmitForm()})function submitForm() {&nbsp; &nbsp; var input = $('#salary');&nbsp; &nbsp; var input1 = $('#bonus');&nbsp; &nbsp; &nbsp;console.log("before salary",input.val())&nbsp; &nbsp; &nbsp;console.log("before bonus",input1.val())&nbsp; &nbsp; input.val(input.val().replace(/,/g, ''));&nbsp; &nbsp; input1.val(input1.val().replace(/,/g, ''));&nbsp; &nbsp; &nbsp; &nbsp; console.log("after",input.val())&nbsp; &nbsp; &nbsp; &nbsp; console.log("after",input1.val())}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form method="POST" class="form-horizontal&nbsp; mb-5" onsubmit="submitForm()">&nbsp; &nbsp; <input type="text" name="Salary" id="salary" class="textInput form-control commanumber">&nbsp; &nbsp; <input type="text" name="Bonus" id="bonus" class="textInput form-control commanumber">&nbsp; &nbsp; <input type="text" name="Overtime" class="textInput form-control commanumber">&nbsp; &nbsp; &nbsp; <input type="submit" name="submit" id="btn"></form>

森栏

您不需要选择所有元素,而是需要通过方法逐一选择jQuery.each。function submitForm(e) {&nbsp; e.preventDefault(); // remove this in your project!&nbsp; $('.commanumber').each((index, input) => {&nbsp; &nbsp; const $input = $(input);&nbsp; &nbsp; $input.val($input.val().replace(/,/g, ''));&nbsp; });}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form method="POST" class="form-horizontal&nbsp; mb-5" onsubmit="submitForm(event)">&nbsp; <input type="text" name="Salary" class="textInput form-control commanumber" value="1,234,567">&nbsp; <input type="text" name="Bonus" class="textInput form-control commanumber" value="200,000">&nbsp; <input type="text" name="Overtime" class="textInput form-control commanumber" value="300,000">&nbsp; <input type="submit" value="Remove Commas!" /></form>

梦里花落0921

您的输入变量是一个元素数组,因此您需要一个一个地选择以删除逗号。我为您提供 3 种方法:function submitForm(e) {&nbsp; e.preventDefault();&nbsp; $('.commanumber').each((index, input) => { //1st way&nbsp; &nbsp; &nbsp; const $input = $(input);&nbsp; &nbsp; &nbsp; $input.val($input.val().replace(/,/g, ''));&nbsp; });&nbsp;&nbsp;&nbsp; let input = document.querySelectorAll('.commanumber'); //2nd way&nbsp; input.forEach(index => {&nbsp; &nbsp; &nbsp; $(index).val($(index).val().replace(/,/g, ''));&nbsp; });&nbsp;&nbsp;&nbsp; $.map(input, function(content, i) { //3rd way&nbsp; &nbsp; &nbsp; $(content).val($(content).val().replace(/,/g, ''));&nbsp; });}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form method="POST" class="form-horizontal&nbsp; mb-5" onsubmit="submitForm(event)">&nbsp; &nbsp; <input type="text" name="Salary" class="textInput form-control commanumber" value="8,210,010">&nbsp; &nbsp; <input type="text" name="Bonus" class="textInput form-control commanumber" value="1,058,543">&nbsp; &nbsp; <input type="text" name="Overtime" class="textInput form-control commanumber" value="16,789">&nbsp; &nbsp; <input type="submit" value="Remove Commas!" /></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript