0
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script>
function baseCalculate(slider,sqft,plumbing,credit){
    var cost = document.getElementById("cost");
    var slideval = document.getElementById("slideval");
    var sqft = document.getElementById("sqft");
    var plumbing = document.getElementsByName("plumbing");
    var credit = document.getElementsByName("Credit");
    var price = slider;
    cost.innerHTML = "$"+(((sqft * price) + plumbing) * credit);
    slideval.innerHTML = slider;
}
</script>
</head>

<body>
<h2>Job Estimator</h2>
How many square feet? 
<input type="text" id="sqft"><br>
Price per Square Foot? <input type="range" min="55" max="80" value="70" step="1 
onChange="baseCalculate(this.value,'sqft','plumbing','Credit')" style="width:500px;">

<span id="slideval">70</span>
<p>
 <legend>Will there be plumbing?</legend> <label>
    <input type="radio" name="plumbing" value="150" id="plumbing_0">
    yes</label>
  <br>
  <label>
    <input type="radio" name="plumbing" value="0" id="plumbing_1">
    no</label>
  <br>
  <label>
    <input type="radio" name="plumbing" value="-150" id="plumbing_2">
    promo</label>
  <br>
</p>
<p><legend>Paying with credit card?</legend>
  <label>
    <input type="radio" name="Credit" value="1.06" id="Credit_0">
    Yes</label>
  <br>
  <label>
    <input type="radio" name="Credit" value="1" id="Credit_1">
    No</label>
  <br>
   <label>
     <input type="radio" name="Credit" value=".94" id="Credit_2">
    Promo</label>
  <br>
</p>
<h2>This is the cost of the project: <span id="cost"></span></h2>
</body>
</html>

这是我正在使用的代码。我试图让它乘以平方英尺乘以价格,然后加上管道,然后将所有这些乘以信用。

我在这里想念什么?它返回一个 NaN。

感谢您提供的任何帮助。

4

1 回答 1

0

您必须使用textContent获取文本值并执行计算

var cost = document.getElementById("cost").textContent;
var slideval = document.getElementById("slideval").textContent;
var sqft = document.getElementById("sqft").textContent;

以及输入元素的

var plumbing = document.getElementsByName("plumbing")[0].value;
var credit = document.getElementsByName("Credit")[0].value;

getElementsByName将返回数组,在这里我取了第一个值[0]

你的情况你需要做一个for循环并检查checked属性并获取它value的计算

于 2013-10-17T15:11:40.627 回答