【问题标题】:Javascript: assigning an input letter a specific valueJavascript:为输入字母分配特定值
【发布时间】:2017-03-21 04:58:46
【问题描述】:

我在为输入分配特定值时遇到问题。例如,当用户通过 TicketType 提示输入字母时,我希望将该字母转换为数字。所以说他们输入“B”作为他们的 TicketType,然后我希望将“B”转换为 50,以便我以后可以计算总成本。目前 TotalPayment 只显示 NaN,我很困惑。

这是我的 JavaScript 代码:

function ticket() {

    var TicketType;
    TicketType = prompt("Please enter the type of ticket you require!");
    document.write("<br>");
    var TicketQty;
    TicketQty = prompt("Please enter the number of tickets you require!");
    TicketQty = parseInt(TicketQty);
    document.write("Number of Tickets:" + TicketQty);
    document.write("<br>");
    var TotalPayment =(TicketPrice) * (TicketQty);
    document.write("Total Payment is:" + TotalPayment);
    var TicketPrice;
    TicketPrice = parseInt(TicketPrice);

    if (TicketType == A) {
        TicketPrice == 100;
    }
    else if (TicketType == B) {
        TicketPrice == 75;
    }
    else if (TicketType == C){
        TicketPrice == 50;
    }
    else {
        document.write("Invalid Ticket Type");
    }
}

这是我的 HTML 代码:

<html>
<title>Ticket</title>
<h1>Ticket</h1>
<script src="test.js">   </script>
<script>calculate()</script>
</body>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    尝试以下方法:

    var TicketType;
    var TicketQty;
    var TicketPrice;
    function calculateticket() {
        TicketType = prompt("Please enter the type of ticket you require!");
        document.write("<br>");
        TicketQty = prompt("Please enter the number of tickets you require!");
        testType();
    }
    
    calculateticket();
    function testType(){
        if(TicketType =='A' || TicketType == 'B' || TicketType == 'C'){
            if (TicketType == 'A') {
                TicketPrice = 100;
            } else if (TicketType == 'B') {
                TicketPrice = 75;
            }  else if (TicketType == 'C'){
              TicketPrice = 50;
            }
            
            TicketQty = parseInt(TicketQty);
            document.body.innerHTML += '<span id="text1"></span>';
            document.getElementById('text1').innerHTML = 'Total Payment is: '+TicketQty;
            document.write("<br>");
            var TotalPayment =(TicketPrice) * (TicketQty);
            document.body.innerHTML += '<span id="text2"></span>';
            document.getElementById('text2').innerHTML = 'Total Payment is: '+TotalPayment;
            TicketPrice = parseInt(TicketPrice);
        }
        else {
            if(document.getElementById('text1') != null){
                document.getElementById('text1').innerHTML = '';
            }
            if(document.getElementById('text2') != null){
                document.getElementById('text2').innerHTML = '';
            }
            document.write("Invalid Ticket Type");
        }
    }

    【讨论】:

    • 非常感谢!!还有一个问题:如果页面显示无效的票种,我可以让它只显示无效的票种而不显示其他吗?
    • 嗨 Mithrandir,现在试试 sn-p。我已经更改了代码。如果有任何问题,请告诉我。
    【解决方案2】:

    你需要改变两件事:

    1:字符串比较

    if (TicketType == A)
    

    if (TicketType == 'A')
    

    2:分配错误

    TicketPrice == 100
    

    TicketPrice = 100
    

    【讨论】:

      【解决方案3】:

      您的字符串比较完全错误。我限制了我的回答,因为这就是你要问的。

      if (TicketType == A) {
      

      需要:

      if (TicketType === "A") {
      

      此外,您必须清楚分配“=”与比较“==”。

      整个部分应该更像这样:

      if (TicketType === "A") {
          TicketPrice = 100;
      } else if (TicketType === "B") {
          TicketPrice = 75;
      } else if (TicketType === "C")
          TicketPrice = 50;
      else {
          document.write("Invalid Ticket Type");
      }
      

      当你有一个链式 if-then-elseif 时查看 switch 语句

      您的代码还有其他问题。 Here's a jsfiddle 进行了一些修复和改进。

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        相关资源
        最近更新 更多