Javascript 根据百分比拒绝输入
我试着绕过脚本,但它没有像我预期的那样工作(在Google Webapp脚本HTML表单上工作),如果预付款低于价格的30%或超过价格的100%,我想拒绝输入,答案应该是无效输入。此脚本中应更改哪些内容Javascript 根据百分比拒绝输入,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我试着绕过脚本,但它没有像我预期的那样工作(在Google Webapp脚本HTML表单上工作),如果预付款低于价格的30%或超过价格的100%,我想拒绝输入,答案应该是无效输入。此脚本中应更改哪些内容 //------------------------Process---------------------------------------// document.getElementById("btn").addEventListener("click&quo
//------------------------Process---------------------------------------//
document.getElementById("btn").addEventListener("click", doStuff);
function doStuff() {
const rate = 0.155;
const vprice = document.getElementById("price").value;
const downPayment = document.getElementById("DownPay").value;
const period = document.getElementById("app").value;
var nf = new Intl.NumberFormat(); //number format
const res = nf.format(Math.round ((vprice - downPayment) * rate / period));
document.getElementById("res").innerHTML = "Your Estimated Monthly Rental: "+ res;
google.script.run.userClicked({
vprice,
downPayment,
rate,
period,
res
});
}
解决方案:
<script>
//dropdown for html----------------
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
});
//---------------------------------
//------------------------Process---------------------------------------//
document.getElementById("btn").addEventListener("click", doStuff);
function doStuff() {
const rate = 0.155;
const vprice = document.getElementById("price").value;
const downPayment = document.getElementById("DownPay").value;
const period = document.getElementById("app").value;
var nf = new Intl.NumberFormat(); //number format
const res = nf.format(Math.round ((vprice - downPayment) * rate / period));
if(parseInt(downPayment) < 0.3*parseInt(vprice) || parseInt(downPayment) > parseInt(vprice)) {
document.getElementById("res").innerHTML = "Invalid Input";
}
else {
document.getElementById("res").innerHTML = "Your Estimated Monthly Rental: "+ res;
google.script.run.userClicked({
vprice,
downPayment,
rate,
period,
res
});
}
}
</script>
//html下拉列表----------------
document.addEventListener('DOMContentLoaded',function(){
var elems=document.querySelectorAll('select');
var实例=M.FormSelect.init(elems);
});
//---------------------------------
//------------------------过程---------------------------------------//
document.getElementById(“btn”)。addEventListener(“单击”,doStuff);
函数doStuff(){
常数率=0.155;
const vprice=document.getElementById(“价格”).value;
常量首付=document.getElementById(“首付”).value;
const period=document.getElementById(“应用”)值;
var nf=new Intl.NumberFormat();//数字格式
const res=nf.格式(Math.round((v价格-首付)*费率/期间));
如果(parseInt(首期付款)<0.3*parseInt(价格)| | parseInt(首期付款)>parseInt(价格)){
document.getElementById(“res”).innerHTML=“无效输入”;
}
否则{
document.getElementById(“res”).innerHTML=“您的估计月租金:”+res;
google.script.run.userClicked({
vprice,
首期付款,
比率,
时期
物件
});
}
}
在定义首付后放置该行:if(downPayment1){document.getElementById(“res”).innerHTML=“无效输入”;return;)
@Marios我在const-downPayment变量后添加了你的行,当我刷新表单时,句点下拉框消失了。我做错了什么吗?userClicked
做了什么?@Marios Nothing你能共享文件副本吗?我仍然遇到同样的问题,下拉框没有出现在我的HTML表单a中在添加该行之后:(