“如果” javascript 中的声明 &在标签中显示结果

发布于 2024-09-25 10:06:12 字数 3087 浏览 1 评论 0原文

我有一个注册表单,客户可以通过下拉列表选择他们想要的套餐。然后,他们通过选择“是”或“否”单选按钮来选择是否希望利用多年折扣。如果选择“是”单选按钮,则会显示一个下拉列表(使用 JavaScript),其中包含 1-3 年的选择。 我对 javascript 一点也不熟悉,接下来我需要做的是,当客户选择第 1、2、3 年进行多年折扣时,计算此折扣,并在下拉列表旁边的标签中显示或隐藏此标签,如果客户选择了“否”或“多年折扣”。

这是我现有的代码: 隐藏年度折扣下拉列表的 Javascript

<script type='text/javascript'>

$(function(){

 $('#discountselection').hide();

    $('#No').click(function(){
        $('#discountselection').hide();
    });

    $('#Yes').click(function(){
        $('#discountselection').show();
    });

});

</script>

下面是 HTML 代码:

        <td width="343">Package*</td>

        <td colspan="4">

        <select class="purple" name="package">

            <option value="Standard">Standard - &euro;55 Monthly</option>

            <option value="Premium" selected="selected">Premium - &euro;99 Monthly</option>

            <option value="Platinum">Platinum - &euro;149 Monthly</option>

        </select>

        </td>

        <tr>

        <td width="343">Would You like to avail of our multiyear discounts?*

        <br />See <a href="#dialog" name="modal">Pricing</a> for more details

        </td>

        <td colspan="4">

            <input name="discount" type="radio" id="Yes" value="Yes" />Yes

            <input name="discount" type="radio" id="No" value="No" checked="checked" />No<br />  

            <select name="discountselection" class="purple" id="discountselection">

                <option value="1" selected="selected">1 Year</option>

                <option value="2">2 Years</option>

                <option value="3">3 Years</option>  

            </select>                  

        </td>

这将是我需要在 javascript 中执行以计算价格的 IF 语句:

if package == "standard" && discountselection == "1"
{
    cost = 45 * 12;
    //display cost in label
}   
elseif package == "standard" && discountselection =="2"
{
    cost = 45 * 24; 
    //display cost in label
}
elseif package == "standard" && discountselection =="3"
{
    cost = 45 * 36;
    //display cost in label
}
elseif package == "premium" && discountselection =="1"
{
    cost = 85 * 12; 
    //display cost in label
}
elseif package == "premium" && discountselection =="2"
{
    cost = 85 * 24;
    //display cost in label
}
elseif package == "premium" && discountselection =="3"
{
    cost = 85 * 36;
    //display cost in label
}
elseif package == "platinum" && discountselection =="1"
{
    cost = 134 * 12;
    //display cost in label 
}
elseif package == "platinum" && discountselection =="2"
{
    cost = 134 * 24;
    //display cost in label
}
else package == "platinum" && discountselection =="3"
{
    cost = 134 * 36;
    //display cost in label
}

我只是不确定如何在 Javascript 中创建此 IF 语句。如果有人可以提供任何建议、帮助或教程链接来帮助我,我将非常感激。

I have a registration form where a customer selects their desired package via a dropdownlist. They then select whether they wish to avail of a multiyear discount by choosing Yes or No radio buttons. If the yes radio button is selected, a dropdownlist is shown (using javascript) with a selection of years 1-3.
I'm not familar at all with javascript, and what I need to do next is when the customer selects year 1, 2, 3 for multiyear discounts, to calculate this discount, and show in a label beside the dropdownlist OR hide this label if the customer has selected no to multiyear discounts.

Here is my existing code:
Javascript to hide the yearly discount dropdownlist

<script type='text/javascript'>

$(function(){

 $('#discountselection').hide();

    $('#No').click(function(){
        $('#discountselection').hide();
    });

    $('#Yes').click(function(){
        $('#discountselection').show();
    });

});

</script>

And here is the HTML code:

        <td width="343">Package*</td>

        <td colspan="4">

        <select class="purple" name="package">

            <option value="Standard">Standard - €55 Monthly</option>

            <option value="Premium" selected="selected">Premium - €99 Monthly</option>

            <option value="Platinum">Platinum - €149 Monthly</option>

        </select>

        </td>

        <tr>

        <td width="343">Would You like to avail of our multiyear discounts?*

        <br />See <a href="#dialog" name="modal">Pricing</a> for more details

        </td>

        <td colspan="4">

            <input name="discount" type="radio" id="Yes" value="Yes" />Yes

            <input name="discount" type="radio" id="No" value="No" checked="checked" />No<br />  

            <select name="discountselection" class="purple" id="discountselection">

                <option value="1" selected="selected">1 Year</option>

                <option value="2">2 Years</option>

                <option value="3">3 Years</option>  

            </select>                  

        </td>

This would be the IF statement I would need to execute within the javascript to calculate the price:

if package == "standard" && discountselection == "1"
{
    cost = 45 * 12;
    //display cost in label
}   
elseif package == "standard" && discountselection =="2"
{
    cost = 45 * 24; 
    //display cost in label
}
elseif package == "standard" && discountselection =="3"
{
    cost = 45 * 36;
    //display cost in label
}
elseif package == "premium" && discountselection =="1"
{
    cost = 85 * 12; 
    //display cost in label
}
elseif package == "premium" && discountselection =="2"
{
    cost = 85 * 24;
    //display cost in label
}
elseif package == "premium" && discountselection =="3"
{
    cost = 85 * 36;
    //display cost in label
}
elseif package == "platinum" && discountselection =="1"
{
    cost = 134 * 12;
    //display cost in label 
}
elseif package == "platinum" && discountselection =="2"
{
    cost = 134 * 24;
    //display cost in label
}
else package == "platinum" && discountselection =="3"
{
    cost = 134 * 36;
    //display cost in label
}

I am just unsure how to create this IF statement within Javascript. If anyone could offer any advice, help, or links to tutorials that could help me in this, I'd be very grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-10-02 10:06:12

在 JavaScript 中,else 和 if 必须是单独的单词。在 javascript 中,if 语句的条件必须或应该括在括号中。所以:

if(package == "standard" && discountselection == "1")
{
    cost = 45 * 12;
}   
else if(package == "standard" && discountselection =="2")
{
    cost = 45 * 24; 
}

等等。但是,如果我是你,我会考虑做一些更自动化的事情。我讨厌重复打字!

var discount = {1: 12, 2: 24, 3: 36};

var package_prices = {'standard': 45, 'premium': 85, 'platinum': 134 };

var cost = package_prices[package] * discount[discountselection];

现在 cost 应该是正确的,看看您节省的所有精力。

注意:我在这里遵循了您的示例代码,但在您的实际 HTML 中,您的选项值有一个大写的第一个字符,所以实际上这会失败,除非修改 package_prices 变量以适应(例如 'Standard': 45 而不是 'standard': 45

如果您正在寻找有关 Javascript 的良好参考资料,而不是更详细的教程,那么Mozilla 开发者中心,例如,这里是他们的 if/else 文档。

In javascript else and if must be separate words. In javascript the condition to an if statement must or should be enclosed in parentheses. So:

if(package == "standard" && discountselection == "1")
{
    cost = 45 * 12;
}   
else if(package == "standard" && discountselection =="2")
{
    cost = 45 * 24; 
}

And so on. But, if I were you I'd consider doing something a little more automatic. I hate repetitive typing!

var discount = {1: 12, 2: 24, 3: 36};

var package_prices = {'standard': 45, 'premium': 85, 'platinum': 134 };

var cost = package_prices[package] * discount[discountselection];

Now cost should be correct, and look at all the effort you save.

Note: I have followed your example code, here, but in your actual HTML your option values have an upper case first character, so in fact this would fail unless the package_prices variable were modified to suit (e.g. 'Standard': 45 instead of 'standard': 45)

If you're looking for good reference material on Javascript, as opposed to more verbose tutorials, you can't do better than the Mozilla Developer Center, for example here's their if/else documentation.

凹づ凸ル 2024-10-02 10:06:12

我希望我正确理解了您的问题,但是如果用户从您的选择框中选择了一个值,您将尝试显示带有成本的标签。

如果您可以通过使用更改事件(由 jQuery 提供)来完成此操作。

$("#discountselection").change(function()  { 
    var product_cat = $("#package option:selected").val();

    if(product_cat == "standard") {
       product_prefix = 45;
    } else if (product_cat == "premium") {
       product_prefix = 85;
    } else if (product_cat == "platinum") {
       product_prefix = 134;
    }

    var selected_value = $("#discountselection option:selected").val();
    var cost = product_prefix * (12 * selected_value);
    $("#costlabel").val(cost);
}); 

但请注意,您应该在服务器上仔细检查这些值!

I hope that I understood your question correctly, but you're trying to display a label with the costs if the user selected a value from your select box.

If you can accomplish this by using the change event (provided by jQuery).

$("#discountselection").change(function()  { 
    var product_cat = $("#package option:selected").val();

    if(product_cat == "standard") {
       product_prefix = 45;
    } else if (product_cat == "premium") {
       product_prefix = 85;
    } else if (product_cat == "platinum") {
       product_prefix = 134;
    }

    var selected_value = $("#discountselection option:selected").val();
    var cost = product_prefix * (12 * selected_value);
    $("#costlabel").val(cost);
}); 

But please be aware that you should double check these values on the server!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文