1

嗨,这对所有人来说都是新的,但我已经制作了一个程序,您可以在其中订购比萨,它会显示我订购的内容,但我需要在最后一个项目末尾有一个“和”的地方制作它.....例如:你点了一份手抛皮披萨。你有以下配料:意大利辣香肠、汉堡包、青椒蘑菇。我需要它并在那里,但似乎无法将它放在那里。在这里可以做的任何帮助都是我的小程序

<!DOCTYPE html>

    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>PIZZA!</title>

        <script type="text/javascript">
            function vOrder() {
                var strCrust, strOrder, iToppingCount, iMeatCount;

                document.getElementById("taOrder").value = "";

                strCrust = document.getElementById("ddlCrust").value;
                strOrder = "You ordered a pizza with " + strCrust + " crust.\n";

                iToppingCount = 0;
                iMeatCount = 0;
                strToppings = "";


                if (document.getElementById("chkPepperoni").checked) {
                    iToppingCount++;  //same as iToppingCount += 1
                    strToppings += "pepperoni";
                    iMeatCount++;
                }
                if (document.getElementById("chkHamburger").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "hamburger";
                    iMeatCount++;
                }
                if (document.getElementById("chkGreenPeppers").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "green peppers";
                }
                if (document.getElementById("chkMushrooms").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "mushrooms";
                }
                if (document.getElementById("chkOnion").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "onions";
                }
                if (document.getElementById("chkSausage").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "sausage";
                    iMeatCount++;
                }


                if (iToppingCount > 0)
                    strToppings = "You have the following toppings: " + strToppings;
                else
                    strToppings = "Your pizza is a plain cheese.";

                if (iMeatCount > 2)
                    strOrder = "screw you no more then two";
                else

                    strOrder += strToppings;

                document.getElementById("taOrder").value = strOrder;

            }

        </script>

        <style type="text/css">
            hr {
                color:firebrick;
                height:4px;
            }
        </style>

    </head>


    <body>
        <div style="border-style:groove; border-color:firebrick; border-width:5px; margin:auto; width:4.5in; background-color:bisque">
            <form action="pizza_ddl_chk.html">
                <table style="width:100%">
                    <tr>
                        <td style="width:100px">
                            <img src="images/pizza.jpg" width="100" alt="Tasty pizza!" />
                        </td>
                        <td style="font-size:30pt;font-family:Biondi;text-align:center">
                            <span style="color:green">That's</span>  
                            <span style="color:white">A</span>
                            <span style="color:red">Pizza!</span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><hr  /></td>
                    </tr>
                    <tr>
                        <td>Choose your crust:</td>
                        <td>
                            <select id="ddlCrust">
                                <option value="thin">Thin</option>
                                <option value="thick">Thick</option>
                                <option value="hand-tossed">Hand-tossed</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Choose your toppings:</td>
                        <td>
                            <table>
                                <tr>
                                    <td>Pepperoni</td>
                                    <td>
                                        <input type="checkbox" id="chkPepperoni" />
                                    </td>
                                    <td>Sausage</td>
                                    <td>
                                        <input type="checkbox" id="chkSausage" />
                                    </td>
                                    <td>Onion</td>
                                    <td>
                                        <input type="checkbox" id="chkOnion" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Hamburger</td>
                                    <td>
                                        <input type="checkbox" id="chkHamburger" />
                                    </td>
                                    <td>Green Peppers</td>
                                    <td>
                                        <input type="checkbox" id="chkGreenPeppers" />
                                    </td>
                                    <td>Mushrooms</td>
                                    <td>
                                        <input type="checkbox" id="chkMushrooms" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align:center"><input type="button" id="btnOrder" value="Place Your Order" onclick="vOrder()"/></td>
                    </tr>
                     <tr>
                        <td colspan="2"><hr  /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <textarea id="taOrder" cols="49" rows="4"></textarea>
                        </td>
                    </tr>
                </table>

            </form>
        </div>
    </body>
    </html>
4

2 回答 2

0

从语法上讲,您必须用“and”替换最后一个逗号。如果您已经构建了字符串,则只需替换其中最后出现的逗号。如何做到这一点有很多变化,这个版本使用这个答案的修改版本:

function replaceLastInstance(badtext, str, repl) {
    var charpos = str.lastIndexOf(badtext);
    if (charpos<0) return str;
    ptone = str.substring(0,charpos);
    pttwo = str.substring(charpos+(badtext.length));
    return (ptone+repl+pttwo);
}

使用此函数,您可以简单地调用:

var str = 'You ordered a pizza with hand-tossed crust. You have the following toppings: pepperoni, hamburger, green peppers, mushroom.'

var str2 = replaceLastInstance(',', str, ' and')

console.log(str2);

结果是:

您点了一份带有手工煎饼皮的披萨。你有以下配料:意大利辣香肠、汉堡包、青椒和蘑菇。

演示:http: //jsfiddle.net/qVbn5/

于 2013-09-19T05:01:34.217 回答
0

把它分成两部分。

首先,使用您需要的任何逻辑准备您的成分列表:

var ingredients = [];
ingredients.push("cheese");
ingreiendts.push("pepperoni");
ingredients.push("tomato");

然后创建句子:

var toppings;

if (ingredients.length == 0)
  toppings = "";
else if (ingredients.length == 1)
  toppings = ingredients[0];
else
  toppings = ingredients.slice(0, ingredients.length - 1).join(", ") + " and " + ingredients[ingredients.length - 1]

这:ingredients.slice(0, ingredients.length - 1).join(", ")将除最后一种成分之外的所有成分与“,”作为分隔符连接在一起。

" and " + ingredients[ingredients.length - 1]只需将最后一个成分添加到字符串中,中间有文本 ' 和 '

于 2013-09-19T03:59:45.507 回答