admin管理员组文章数量:1405611
I would like to know how to use single method for both inputs in javacript.
I have a form having two input fields, calling a function to format currecny. I need to know how to apply for same function for two different inputs.
formatCurrency(e){
var myinput = e.target.value;
var val = myinput;
val = val.replace(/[^0-9\.]/g,'');
if(val != "") {
var valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
document.getElementById("samount").value = val;
}
formatRCurrency(e){
var myinput = e.target.value;
var val = myinput;
val = val.replace(/[^0-9\.]/g,'');
if(val != "") {
var valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
document.getElementById("tamount").value = val;
}
<input type="text" id="samount" name="samount" class="form-control" @keyup=${this.formatSCurrency} >
<input type="text" id="tamount" name="tamount" class="form-control" @keyup=${this.formatRCurrency} >
How to make a single function can be used for both inputs.
I would like to know how to use single method for both inputs in javacript.
I have a form having two input fields, calling a function to format currecny. I need to know how to apply for same function for two different inputs.
formatCurrency(e){
var myinput = e.target.value;
var val = myinput;
val = val.replace(/[^0-9\.]/g,'');
if(val != "") {
var valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
document.getElementById("samount").value = val;
}
formatRCurrency(e){
var myinput = e.target.value;
var val = myinput;
val = val.replace(/[^0-9\.]/g,'');
if(val != "") {
var valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
document.getElementById("tamount").value = val;
}
<input type="text" id="samount" name="samount" class="form-control" @keyup=${this.formatSCurrency} >
<input type="text" id="tamount" name="tamount" class="form-control" @keyup=${this.formatRCurrency} >
How to make a single function can be used for both inputs.
Share Improve this question asked Apr 23, 2019 at 7:48 SenSen 7733 gold badges14 silver badges35 bronze badges2 Answers
Reset to default 5The e.target
refers to the <input>
you're operating on, so just change the
document.getElementById(...).value = val;
to
e.target.value = val;
and you'll be able to use the same function for both inputs. There's also probably no need for the id
s anymore.
<input type="text" name="samount" class="form-control" @keyup=${this.formatCurrency} >
<input type="text" name="tamount" class="form-control" @keyup=${this.formatCurrency} >
and
formatRCurrency(e){
var value = e.target.value;
value = value.replace(/[^0-9\.]/g,'');
if(value !== "") {
var valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
e.target.value = val;
}
(note that your myinput
variable actually contains a string, not an HTMLInputElement
, which may be confusing - best to use precise variable names)
A simple way to achieve this is to scrutinise the target Element's id
attribute and take action accordingly. For example...
function eventHandler(e) {
var targetID = e.target.id;
switch(targetID) {
case "samount":
/* 'samount' specific process */
break;
default:
/* 'tamount' specific process */
}
}
If you wish to carry out the same process regardless of the active Element then the event.target
property contains a reference to that Element...
function eventHandler(e) {
var element = e.target;
var val = element.value;
// process 'val' etc ....
element.value = new_value;
}
Hope that helps. :)
- switch @ MDN
- event.target @ MDN
本文标签: jqueryHow to use single function for multiple inputs in javascriptStack Overflow
版权声明:本文标题:jquery - How to use single function for multiple inputs in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744253157a2597345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论