Javascript if语句基于下拉菜单值

时间:2012-08-13 22:55:57

标签: javascript html forms drop-down-menu if-statement

我对js中的if语句有疑问。我做了一个声明,但似乎没有用。我想通过单击“按钮1”来检查这些语句。该脚本是在计算总数之前检查是否有任何运输成本。 有人能告诉我如何解决我的问题吗?

这是我的脚本:     

<SCRIPT TYPE="text/javascript">
function berekeningen()
{

if (document.getElementById("DropdownMenu1").value = "Eeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 20)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Adegem" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Kaprijke" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Lembeke" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Oosteeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Sint-Laureins" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Ursel" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Waarschoot" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Zomergem" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Knesselare")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Lovendegem")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Bassevelde")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Maldegem")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }
}
</SCRIPT>

编辑: 我用变量改变了一些脚本,但它不起作用

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.js"></script>

<SCRIPT TYPE="text/javascript">
function berekeningen()
{
   var plaats           = document.getElementById("plaats").value;
   var kosten           = document.getElementById("kosten").value;
   var subtotaal        = document.getElementById("Bestelling_subtotaal").firstChild.data
   var class_1_plaatsen = new Array("Eeklo");
   var class_2_plaatsen = new Array("Adegem", "Kaprijke", "Lembeke", "Oosteeklo",
                                    "Sint-Laureins", "Ursel", "Waarschoot", "Zomergem");
   var class_3_plaatsen = new Array("Knesselare", "Lovendegem", "Bassevelde", "Maldegem");

   if ($.inArray(plaats, class_1_plaatsen) >= 0) {

      // Berekening voor plaatsen in prijscategorie 1 hieronder.

   }
   else if ($.inArray(plaats, class_2_plaatsen) >= 0) {

      // Berekening voor plaatsen in prijscategorie 2 hieronder.

   }
   else {

      // Berekening voor overige plaatsen hieronder.

   }
}
</SCRIPT>

我不想这样做:

  

当var plaats = class_1_plaatsen和subtotaal&lt; 15,然后警告(我们   leveren vanaf最低€15)。但如果subotaal在15到20之间,   那么kosten = 3.但是如果 = 20,则kosten = 0.

     

当var plaats = class_2_plaatsen和subtotaal&lt; 20,然后警告(我们   leveren vanaf最低€20)。但如果subotaal在20到25之间,   那么kosten = 3.但是如果 = 25,那么kosten = 0.

     

当var plaats = class_3_plaatsen和subtotaal&lt; 20,然后警告(我们   leveren vanaf最低€20)。但如果 = 20,则kosten = 5.

Dyasten

2 个答案:

答案 0 :(得分:1)

不应该所有的if语句都有两个==

if  (document.getElementById("DropdownMenu1").value == "Knesselare")

答案 1 :(得分:1)

通常,您不希望在if语句中使用single =符号;它评估为一个新的变量赋值。

而不是= use ==喜欢这样:

if (document.getElementById("DropdownMenu1").value == "Eeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 20)