基于下拉菜单值的 Javascript if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11943436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:49:17  来源:igfitidea点击:

Javascript if statement based on dropdown menu value

javascripthtmlformsdrop-down-menuif-statement

提问by Yasin Palit

I have a question about the if statement in js. I made a statement, but it doesn't seems to work. I want to check these statements from one single click on "button 1". The script is to check if there are any transportation costs before I calculate the total. Can someone tell me how to fix my problem?

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

Here's my script:

这是我的脚本:

<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>

Edit: I changed the script a bit with variables but it doesn't work

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

<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>

I wan't to make this:

我不想做这个:

When var plaats = class_1_plaatsen and subtotaal < 15, then alert (We leveren vanaf minimum 15). But if subtotaal is between 15 and 20, then kosten = 3. But if subtotaal >= 20, then kosten = 0.

When var plaats = class_2_plaatsen and subtotaal < 20, then alert (We leveren vanaf minimum 20). But if subtotaal is between 20 and 25, then kosten = 3. But if subtotaal >= 25, then kosten = 0.

When var plaats = class_3_plaatsen and subtotaal < 20, then alert (We leveren vanaf minimum 20). But if subtotaal >= 20, then kosten = 5.

当 var plaats = class_1_plaatsen 并且小计 < 15 时,则警报(我们调整 vanaf 最小值为 15)。但如果小计在 15 到 20 之间,则 kosten = 3。但如果小计 >= 20,则 kosten = 0。

当 var plaats = class_2_plaatsen 并且 subtotal < 20 时,则警报(我们调整 vanaf 最小值为 20)。但如果小计在 20 到 25 之间,则 kosten = 3。但如果小计 >= 25,则 kosten = 0。

当 var plaats = class_3_plaatsen 并且小计 < 20 时,请保持警惕(我们调整 vanaf 最小值为 20)。但如果小计 >= 20,则 kosten = 5。

Dyasten

达斯腾

回答by KingKongFrog

Shouldn't all your if statements have two ==

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

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

回答by limedrop

Generally you don't want to use the single = sign in an if statement; it evaluates as a new variable assignment.

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

Instead of = use == like this:

而不是 = 使用 == 像这样:

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