问题描述
|
我有以下脚本,其中变量从输入字段获取其值,但是当我运行我的函数时,它不起作用,什么也不返回。我不是JS的新手,所以不确定它是否需要成为函数的一部分*即使我没有运气也尝试过此操作)还是什么...
/////////////////////////////////////////////////////////////////////
// Variables
// Content/SLA
var ContentMinutes = \'\';
var ContentMinutesSelector; // Switch Case
var ServiceLevel = 5;
var NoOfFrames = 2;
// Render Time (Hairier the Better)
var AvgFrameRenderTime = \'\';
var AvgFrameRenderTimeSelector = 10; // Switch Case
var CoresInTest = document.getElementById(\'CoresInTest\').value;
// Other
var EstimatedCoreHours = NoOfFrames * CoresInTest * AvgFrameRenderTimeSelector;
// Cost Estimate
var CostEstimate = ServiceLevel * EstimatedCoreHours;
/////////////////////////////////////////////////////////////////////
// Functions
function CalculateEstimate() {
// Estimate Cost
parseInt(document.getElementById(\"PriceEstimate\").innerHTML=CostEstimate.toFixed(2));
// Estimate Core Hours
parseInt(document.getElementById(\"EstimatedCoreHours\").innerHTML=EstimatedCoreHours.toFixed( 2));
}
我的PriceEstimate和EstimatedCoreHours字段都是空的div,<div id=\"EstimatedCoreHours\"></div>
,如果我为变量定义一个值而不是document.getElementById
,那么我的计算就可以了,所以我认为我必须运行一个函数或更新所有变量的东西吗?
但是如果我设置...
var CoresInTest = document.getElementById(\'CoresInTest\').value;
至
var CoresInTest = 10;
然后工作正常...
它实际上不是我的回报,问题是我的变量arent调用,如果我用一个数字定义它们,那么它将起作用。
解决方法
我想如果要在div中获取计算的数据,则需要执行以下操作。
document.getElementById(\"PriceEstimate\").innerHTML=parseInt(CostEstimate.toFixed(2));
// Estimate Core Hours
document.getElementById(\"EstimatedCoreHours\").innerHTML=parseInt(EstimatedCoreHours.toFixed(2));
如果ѭ6正常工作,则您的代码放置错误。
element7是什么元素?是文本字段吗?如果是的话,该脚本是否在元素渲染之前放置或调用?那么您将不得不重新初始化该变量。
,如果PriceEstimate和EstimatedCoreHours是元素,则应使用value属性
这可能对您有用:
document.getElementById(\"PriceEstimate\").value=parseInt(CostEstimate.toFixed(2),10);
document.getElementById(\"EstimatedCoreHours\").value=parseInt(EstimatedCoreHours.toFixed(2),10);
,如果var CoresInTest = 10;
可以正常工作,那么一定与document.getElementById(\'CoresInTest\').value
有关-那为什么不起作用?它是一个下拉列表吗?不用我们猜测,请告诉我们。