未使用本地存储在表单提交中保存值

时间:2015-05-10 19:02:13

标签: javascript forms local-storage

我想从表单中捕获2个值(产品名称和能耗)并使用本地存储进行存储。

OnClick我想在表单一侧显示存储的值。

工作正常 本地存储设置正在设置中。

问题(在评论函数compareSetup中)

1)我的猜测是我没有从表格本身获得所选产品和能量值。

2)写了显示逻辑但在该行上出错 Uncaught SyntaxError:意外的标识符。 我尝试了两种不同的方式。我是新手因此尝试不同的语法

3)Chrome中的“资源”选项卡显示 callAnnual as undefined callProduct为0

4)内部功能警报不起作用

我错过了什么。逻辑上或语法上或两者兼而有之。 有人可以指导我。谢谢你的时间。

var wattage = {
  'Artic King AEB': 100,
  'Artic King ATMA': 200,
  'Avanti Compact': 300,
  'Bosch SS': 400,
  'Bosch - SHXUC': 100,
  'Asko DS': 200,
  'Blomberg': 300,
  'Amana': 400
};
var annualEnergy = 0;
var dailyEnergyConsumed = 0;

function configureDropDownLists(category, products) {

  var refrigerators = new Array('Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS');
  var dishWasher = new Array('Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana');
  //var wattage = {'Artic King AEB':100,'Artic King ATMA':200,'Avanti Compact':300;'Bosch SS':400,'Bosch - SHXUC':100;'Asko DS':200;'Blomberg':300;'Amana':400}

  switch (category.value) {
    case 'refrigerators':
      products.options.length = 0;
      for (i = 0; i < refrigerators.length; i++) {
        createOption(products, refrigerators[i], refrigerators[i]);
      }
      break;
    case 'dishWasher':
      products.options.length = 0;
      for (i = 0; i < dishWasher.length; i++) {
        createOption(products, dishWasher[i], dishWasher[i]);
      }
      break;
    default:
      products.options.length = 0;
      break;
  }

}

function createOption(ddl, text, value) {
  var opt = document.createElement('option');
  opt.value = value;
  opt.text = text;
  ddl.options.add(opt);
}

function configureProductDropDownLists(product) {
  document.getElementById('wattage').innerText = wattage[product.value];

}

function setConsumption(hrs) {
  setConsumption();

}

dailyEnergyConsumption = function(hrs) {

  dailyEnergyConsumed = 0;
  dailyEnergyConsumed = parseFloat(hrs * parseInt(document.getElementById('wattage').innerText) / 1000).toFixed(2);
  document.getElementById('dailyEnergyConsumptionVal').innerText = dailyEnergyConsumed + " kWh";

}


annualEnergyConsumption = function(days) {

  annualEnergy = 0;


  var allYear = document.getElementById('allYear');
  var halfYear = document.getElementById('halfYear');
  var threeMonths = document.getElementById('threeMonths');
  var oneMonth = document.getElementById('oneMonth');

  if (allYear || days != 365) {
    annualEnergy = parseFloat(dailyEnergyConsumed * parseInt(days)).toFixed(2);
    document.getElementById('annualEnergyConsumption').innerText = annualEnergy + " kWh";

  } else if (days == 182 && !halfYear) {
    annualEnergy = parseFloat(dailyEnergyConsumed * parseInt(days)).toFixed(2);
    document.getElementById('annualEnergyConsumption').innerText = annualEnergy + " kWh";
  } else if (days == 90 && !threeMonths) {
    annualEnergy = parseFloat(dailyEnergyConsumed * parseInt(days)).toFixed(2);
    document.getElementById('annualEnergyConsumption').innerText = annualEnergy + " kWh";
  } else if (days == 30 && !oneMonth) {
    annualEnergy = parseFloat(dailyEnergyConsumed * parseInt(days)).toFixed(2);
    document.getElementById('annualEnergyConsumption').innerText = annualEnergy + " kWh";
  }


}


/*

function compareSetup(){
// I am trying to set the local storage for 2 things : selected [product name and Annual Energy Consumption]. End goal is to use them inorder to plot a chart 


        //initialize the selected product and annual consumption by calling their latest values on submit
		var submittedProduct = document.getElementById("products").options.selectedIndex;
		var submittedAnnualEnergyConsumption = document.getElementById("annualEnergyConsumption").value;
		
		
		//alert(submittedProduct);
		
        //setting the local storage with a key and variable name which was defined above
        localStorage.setItem("callProduct", submittedProduct);
        localStorage.setItem("callAnnual", submittedAnnualEnergyConsumption);
		
		
        
		//get Item with a key and display
		var displayName = localStorage.getItem("callProduct");
		//alert(callProduct);
		
		displayName = document.getElementById("displayName");
		
		
		var displayAnnual = document.getElementById("displayAnnual").innerHTML = localStorage.getItem parseInt(("callAnnual"));
		
		// return false as I dont want the form to submit
		return false;
    }

*/




$(document).ready(function() {

  $("#h1").click(function() {
    $("#onesSelected").show();

    $("#threeSelected").hide();
    $("#sixSelected").hide();
    $("#twentyFourSelected").hide();

  });

  $("#h3").click(function() {
    $("#threeSelected").show();

    $("#onesSelected").hide();
    $("#sixSelected").hide();
    $("#twentyFourSelected").hide();

  });


  $("#h6").click(function() {
    $("#sixSelected").show();

    $("#onesSelected").hide();
    $("#threeSelected").hide();
    $("#twentyFourSelected").hide();

  });
  $("#h24").click(function() {

    $("#twentyFourSelected").show();

    $("#onesSelected").hide();
    $("#threeSelected").hide();
    $("#sixSelected").hide();


  });

});

/*
annualCost = function() {

// utility rate currently is hard coded but this will come from the database and shall be inputted by user once. Utility Rate does not change that often

var utilityRate = 0.11;
var button = document.getElementById('annualCost');

 var annualCost = parseFloat( annualEnergy * utilityRate).toFixed(2);
document.getElementById('annualCostOperation').innerText= "$" + annualCost  ;

}
*/
#leftColumn {

  width: 600px;

  float: left;

}

.placeholderText {

  font: bold 12px/30px Georgia, serif;

}

body {

  padding-left: 45px;

}

#annualEnergyConsumption {

  font: bold 25px arial, sans-serif;

  color: #00ff00;

}

#dailyEnergyConsumptionVal {

  font: bold 25px arial, sans-serif;

  color: #00ff00;

}

#annualCostOperation {

  font: bold 40px arial, sans-serif;

  color: #00ff00;

}

.dailyButInline {

  display: inline;

}

#wattage {

  position: absolute;

  left: 160px;

  top: 130px;

  font: bold 25px arial, sans-serif;

  color: #00ff00;

}

/* mouse over link */

button:hover {

  background-color: #b6b6b6;

}

#onesSelected {

  position: absolute;

  left: 53px;

  top: 246px;

  background-color: #00ff00;

  display: none;

  width: 99px;

  height: 5px;

}

#threeSelected {

  position: absolute;

  left: 156px;

  top: 246px;

  background-color: #00ff00;

  display: none;

  width: 99px;

  height: 5px;

}

#sixSelected {

  position: absolute;

  left: 259px;

  top: 246px;

  background-color: #00ff00;

  display: none;

  width: 99px;

  height: 5px;

}

#twentyFourSelected {

  position: absolute;

  left: 362px;

  top: 246px;

  background-color: #00ff00;

  display: none;

  width: 113px;

  height: 5px;

}
<h2>Annual Energy Consumption and Cost Calculator</h2>

<form method="post" onSubmit="return compareSetup()">

  <div id="leftColumn">

    <div>
      <span class="placeholderText">Choose Category</span>
      <span>
		<select id="ddl" onchange="configureDropDownLists(this,document.getElementById('products'))">
			<option value="">Select a Category</option>
			<option value="refrigerators">Refrigerators</option>
			<option value="dishWasher">DishWasher</option>
			</select>
		</span>
      </br>

      <span class="placeholderText">Select a Product</span>
      <span>
		<select id="products" onchange="configureProductDropDownLists(this)">
				<option selected>--------------------------</option>
		</select>
		</span>


    </div>

    <div>
      <span class="placeholderText">Wattage</span>
      <span id="wattage">---</span>
      </br>
      </br>
    </div>

    <div id="buttonBoundary">
      <div class="placeholderText">Estimated Daily Use</div>

      <div class="dailyButInline">
        <button type="button" id="h1" onclick="dailyEnergyConsumption(1)">Not a Lot</br>1 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button type="button" id="h3" onclick="dailyEnergyConsumption(3)">Average</br>3 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button type="button" id="h6" onclick="dailyEnergyConsumption(6)">A Lot</br>6 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button type="button" id="h24" onclick="dailyEnergyConsumption(24)">Always On</br>24 hours per day</button>
      </div>


      <div id="onesSelected"></div>
      <div id="threeSelected"></div>
      <div id="sixSelected"></div>
      <div id="twentyFourSelected"></div>

      </br>
      </br>


    </div>
    <div>
      <span class="placeholderText">Daily Energy Consumption</span>
      </br>
      <div id="dailyEnergyConsumptionVal">---</div>
      </br>
    </div>


    <div>
      <span class="placeholderText">Estimated Yearly Use</span>
      </br>

      <input type="radio" name="usageRadio" value="AllYear" id="allYear" onclick="annualEnergyConsumption(365)" />
      <label for="allYear">All year</label>

      <input type="radio" name="usageRadio" value="halfYear" id="halfYear" onclick="annualEnergyConsumption(182)" />
      <label for="halfYear">6 Months</label>

      <input type="radio" name="usageRadio" value="threeMonths" id="threeMonths" onclick="annualEnergyConsumption(90)" />
      <label for="threeMonths">3 Months</label>


      <input type="radio" name="usageRadio" value="oneMonth" id="oneMonth" onclick="annualEnergyConsumption(30)" />
      <label for="oneMonth">1 Month</label>
      <!-- <div id="daysUsed"><input type="number" id="hour" maxlength="2" min="1" onchange="annualEnergyConsumption(this.value)"></br> -->

    </div>
    </br>
    <div>
      <span class="placeholderText">Energy Consumption</span>
      </br>
      <div id="annualEnergyConsumption">---</div>
      </br>
    </div>

    <input type="submit" value="Save Product" />
  </div>

  <div id="right">
    <div id="displayName">Selected Product 1</div>
    <div id="displayAnnual">Selected Product1's Annual Consumption</div>

  </div>
  <!--
<div id="right">

</form>
<div>
		<span class="placeholderText">Enter your Utility Rate per Kw/h</span></br>
		<span><input type="number" id="utilityRate" /></span>
		</br></br>
</div>


<div>
		<span class="placeholderText"><button id="annualCost" onclick='annualCost()'>Annual Cost to Operate</button></span></br>
		<span id="annualCostOperation" /></span>
</div>
</div>

-->

</form>

1 个答案:

答案 0 :(得分:0)

对于未定义的'$'的范围访问应该是一个死的,以解释为什么代码的其余部分不起作用。一旦你在适当的范围内调用了'$'位代码就行了。