所以我有两个输入文本字段和两个选项选择字段,货币兑换计算器。第一个输入字段和两个选择选项的值用于计算最终值。
我想要实现的是用户输入左输入 - 例如“20a” - 修剪“a”或任何其他字符串并继续计算值。
提前非常感谢。这是我的代码:
function calculusSmaller(cur1, cur2) {
calculatedCurrency.value = parseFloat(currVal * (cur1 / cur2)).toFixed(2);
}
function calculusBiger(cur1,cur2) {
calculatedCurrency.value = parseFloat(currVal * (cur1 * cur2)).toFixed(2);
}
//adjusting values if empty or NaN
if (!isNaN(placeholderCurrency.value)) {
placeholderCurrency.parentNode.removeAttribute('data-error');
}else {
//HERE is the code needed, trimming the String.
placeholderCurrency.parentNode.setAttribute('data-error', 'Please enter a number');
mainCurrency();
}
if (placeholderCurrency.value == "") {
resetValues();
}
function mainCurrency(var1, var2) {
var1 = firstValue;
var2 = secondValue;
switch(var1 + "|" + var2) {
case "usd|rsd":
calculusBiger(usd,rsd);
break;
case "eur|rsd":
calculusBiger(eur,rsd);
break;
case "rsd|usd":
calculusSmaller(rsd,usd);
break;
case "rsd|eur":
calculusSmaller(rsd,eur);
break;
case "eur|usd":
calculusSmaller(eur,usd);
break;
case "usd|eur":
calculusSmaller(usd,eur);
break;
default:
resetValues();
}
console.log('test');
}
mainCurrency();
这是HTML:
<select name="selectedCurrency">
<option class="" data="0" value="rsd">Rsd</option>
<option class="" data = "2" value="eur" selected="selected" >Eur</option>
<option class="" data = "1" value="usd" >Usd</option>
</select>
<input value="1" type="text" name="placeholder-currency">
<select name="currentCurrency">
<option class="" data="0" selected="selected" value="rsd">Rsd</option>
<option class="" data="2" value="eur">Eur</option>
<option class="" data="1" value="usd">Usd</option>
</select>
<input type="text" value="1" name="calculated-currency">
答案 0 :(得分:0)
这是一个使用onkeypress的例子。
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<select name="selectedCurrency">
<option class="" data="0" value="rsd">Rsd</option>
<option class="" data="2" value="eur" selected="selected">Eur</option>
<option class="" data="1" value="usd">Usd</option>
</select>
<input value="1" type="text" name="placeholder-currency" onkeypress="return isNumber(event)">
<select name="currentCurrency">
<option class="" data="0" selected="selected" value="rsd">Rsd</option>
<option class="" data="2" value="eur">Eur</option>
<option class="" data="1" value="usd">Usd</option>
</select>
<input type="text" value="1" name="calculated-currency" onkeypress="return isNumber(event)">
<script>
function calculusSmaller(cur1, cur2) {
calculatedCurrency.value = parseFloat(currVal * (cur1 / cur2)).toFixed(2);
}
function calculusBiger(cur1, cur2) {
calculatedCurrency.value = parseFloat(currVal * (cur1 * cur2)).toFixed(2);
}
//adjusting values if empty or NaN
if (!isNaN(placeholderCurrency.value)) {
placeholderCurrency.parentNode.removeAttribute('data-error');
} else {
//HERE is the code needed, trimming the String.
placeholderCurrency.parentNode.setAttribute('data-error', 'Please enter a number');
mainCurrency();
}
if (placeholderCurrency.value == "") {
resetValues();
}
function mainCurrency(var1, var2) {
var1 = firstValue;
var2 = secondValue;
switch (var1 + "|" + var2) {
case "usd|rsd":
calculusBiger(usd, rsd);
break;
case "eur|rsd":
calculusBiger(eur, rsd);
break;
case "rsd|usd":
calculusSmaller(rsd, usd);
break;
case "rsd|eur":
calculusSmaller(rsd, eur);
break;
case "eur|usd":
calculusSmaller(eur, usd);
break;
case "usd|eur":
calculusSmaller(usd, eur);
break;
default:
resetValues();
}
console.log('test');
}
function isNumber(event) {
event = (event) ? event : window.event;
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode !== 46) return false;
return true;
}
mainCurrency();
</script>
</body>
</html>
&#13;