有人可以帮我写一个JavaScript函数,说明如果选择的日期是从当前日期开始超过30天,那么我的费用计算器加20美元吗?任何帮助将不胜感激我非常新,不知道从哪里开始我找不到任何相似的例子..
这是文本框/日历:
<cfinput
type="datefield"
name="purchasedate"
width="130"
required="yes"
message="Please enter purchase date."
value="#dateformat(now(),"mm/dd/yyyy")#"
>
单选按钮决定价格:
var title_prices = new Array();
title_prices["MCO"]=78.25;
title_prices["FL Title"]=78.25;
title_prices["OOS Title"]=88.25;
function getProofOfOwnership()
{
var proofOfOwnership=0;
var theForm = document.forms["form"];
var ownerShip = theForm.elements["ownership"];
for(var i = 0; i < ownerShip.length; i++)
{
if(ownerShip[i].checked)
{
proofOfOwnership = title_prices[ownerShip[i].value];
}
} return proofOfOwnership;
}
这会调用所有功能并将费用加在一起:
function calculateTotal()
{
var titleFees = getProofOfOwnership() + (Function checking date);
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Estimated Transfer Fees $"+titleFees;
}
答案 0 :(得分:1)
此函数采用日期参数,如果日期超过30天,则返回20;如果日期在30天内或将来,则返回0。
function getDatePrice(date) {
if (Object.prototype.toString.call(date) !== '[object Date]') {
//If passed date is undefined or not valid, use today's date
date = new Date();
}
var today = new Date();
var diffMilli = today - date;
//Difference in milliseconds, need to convert to days
var diffDays = diffMilli * 1000 * 60 * 60 * 24;
if (diffDays > 30) {
return 20;
}
else {
return 0;
}
}
行动起来:http://jsfiddle.net/rgjfwdpx/
编辑:要绑定代码,您需要从<cinput>
标记中检索值。我不熟悉它们,但这是我对如何做到这一点的快速猜测。这可能不对。基本上你需要从输入中获取文本值,然后将其解析为新的日期。
function calculateTotal()
{
//Get date from form. I've never worked with <cinput>, so this is just my guess
var theForm = document.forms["form"];
var purchasedate = theForm.elements["purchasedate"];
var date = new Date(purchasedate.value);
var titleFees = getProofOfOwnership() + getDatePrice(date);
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Estimated Transfer Fees $"+titleFees;
}