-2

我有一个标准计算 javascript,现在我需要根据用户选择的值添加多个计算 - 似乎无法让它工作......!有没有机会有人可以看看并告诉我我哪里出错了???

<script type="text/javascript">
function calculate() {
  var noPeople = document.ContactForm.noPeople.value;
  var ddl = document.getElementById("menuType");
  var selectedValue = ddl.options[ddl.selectedIndex].value;
  var noPeople_excess = (noPeople-6);

  if (selectedValue == 3)  {
    var basic = 337;
    var marchCost = (50*noPeople);
    if (marchCost <= basic)         {
      document.ContactForm.marchCost.value = 337;
      document.ContactForm.marchCostVin.value = basic +(noPeople*25)
    } else {
      document.ContactForm.marchCost.value = basic +(noPeople_excess*34);
      document.ContactForm.marchCostVin.value = basic +(noPeople_excess*34)+(noPeople*25)
    }
  } else (selectedValue == 4)  {
    var basic = 368;
    var marchCost = (55*noPeople);
    if (marchCost <= basic) {
      document.ContactForm.marchCost.value = 368;
      document.ContactForm.marchCostVin.value = basic +(noPeople*25)
    } else {
      document.ContactForm.marchCost.value = basic +(noPeople_excess*44);
      document.ContactForm.marchCostVin.value = basic +(noPeople_excess*44)+(noPeople*25)
    }
  } else (selectedValue == 5) {
    var basic = 419;
    var marchCost = (60*noPeople);
    if (marchCost <= basic) {
      document.ContactForm.marchCost.value = 419;
      document.ContactForm.marchCostVin.value = basic +(noPeople*25)
    } else {
      document.ContactForm.marchCost.value = basic +(noPeople_excess*54);
      document.ContactForm.marchCostVin.value = basic +(noPeople_excess*54)+(noPeople*25)
    }
  }
}
</script>
4

3 回答 3

1

This question was indeed many times answered, however here is a simple solution for you:

    //get select value and assing to an input element
function setValue(selectId, elemId) {
    var thisID = selectId, 
    valueSelected = document.getElementById(thisID).value
    console.log(thisID + ' has value of ' + valueSelected);
    document.getElementById(elemId).value = valueSelected;
}

http://plnkr.co/edit/rnenKVaNcJ9RhqLzUaWi

regards.

于 2013-09-26T09:32:06.877 回答
0

我可能对这两个 DDL 感到困惑,但是选择的值应该是 selectedIndex 以与整数进行比较:

var selectedValue = ddl.selectedIndex;

或者条件应该是检查字符串:

if (selectedValue == "3"){ 
于 2013-09-26T09:27:47.030 回答
0

你的 else 缺少一个 if .. 例如: else if (selectedvalue==4)

我建议使用 switch(selectedvalue) { case 3: ... break; case 4: ...etc 在这种情况下会更容易阅读,特别是如果您添加更多选项。

于 2013-09-26T09:33:44.660 回答