2017-10-18 3 views
0

JavaScript 및 HTML 학습을 시작한 지 얼마되지 않아 현재 프로젝트의 피자 배달 프로그램을 작성 중입니다. 사람이 주문서를 작성한 후 (양식에 입력하고 주문 버튼을 클릭하면) 텍스트 영역으로 출력됩니다.Textarea는 (버튼을 클릭 할 때) 지우기를 원하지 않습니다.

누군가 다른 사람이 주문한 경우 (주문 버튼을 다시 클릭하면) 텍스트 영역을 지워야하지만 취소되지는 않습니다. 저는 SO 및 W3School에서 찾은 수많은 것들을 시도했지만 텍스트 영역을 지우고 싶지 않습니다. 텍스트 영역<body>ID 내에서 내 코드하단에 (거의상단 라인들 orderPizzas이을 기능에) :

코드 빈 텍스트 영역에 대한 세부 사항

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

내 코드는 다음과 같습니다.

var pizzaCount = 0; 
 
var gourmetPrice = 15.50; 
 
var standardPrice = 9.50; 
 
var deliveryCharge = 5; 
 
var TotalPrice; 
 
var name; 
 
var adress; 
 
var phoneNumber = 10; 
 
var gourmetCount = 0; 
 
var standardCount = 0; 
 
var orderDetails = ''; 
 

 
function orderPizzas() { 
 
    var customerDetails; 
 
    var i = 0; 
 
    var j = 0; 
 

 
    TotalPrice = 0; 
 
    phoneNumber = ''; 
 

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

 
    var arrStandardPizza = new Array() 
 
    arrStandardPizza[0] = new Array(); 
 
    arrStandardPizza[0]['name'] = 'Hawaiian'; 
 
    arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value); 
 
    arrStandardPizza[1] = new Array(); 
 
    arrStandardPizza[1]['name'] = 'Cheese'; 
 
    arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value); 
 
    arrStandardPizza[2] = new Array(); 
 
    arrStandardPizza[2]['name'] = 'Veggie'; 
 
    arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value); 
 
    arrStandardPizza[3] = new Array(); 
 
    arrStandardPizza[3]['name'] = 'Supreme'; 
 
    arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value); 
 
    arrStandardPizza[4] = new Array(); 
 
    arrStandardPizza[4]['name'] = 'Pepperoni'; 
 
    arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value); 
 

 
    var arrGourmetPizza = new Array() 
 
    arrGourmetPizza[0] = new Array(); 
 
    arrGourmetPizza[0]['name'] = 'Meatlovers'; 
 
    arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value); 
 
    arrGourmetPizza[1] = new Array(); 
 
    arrGourmetPizza[1]['name'] = 'Chicken'; 
 
    arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value); 
 
    arrGourmetPizza[2] = new Array(); 
 
    arrGourmetPizza[2]['name'] = 'Prawn'; 
 
    arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value); 
 

 
    standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount']; 
 
    gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount']; 
 
    pizzaCount = standardCount + gourmetCount; 
 

 
    if (pizzaCount > 12) { 
 

 
    alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount); 
 

 
    } else { 
 

 
    while (i < 5) { 
 

 
     if (arrStandardPizza[i]['amount'] > 0) { 
 

 
     orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount']; 
 

 
     } 
 

 
     i++; 
 

 
    } 
 
    while (j < 3) { 
 

 
     if (arrGourmetPizza[j]['amount'] > 0) { 
 

 
     orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount']; 
 

 
     } 
 

 
     j++; 
 

 
    } 
 

 
    if (document.getOrderMethod.method.value == 'Delivery') { 
 

 
     name = prompt('What is your name?'); 
 
     adress = prompt('What is your adress?'); 
 

 
     while (phoneNumber.toString().length !== 10) { 
 

 
     phoneNumber = prompt('What is your phone number?'); 
 

 
     } 
 
     customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber; 
 

 
     TotalPrice = deliveryCharge; 
 

 
    } else { 
 

 
     name = prompt('What is your name?'); 
 
     customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name; 
 

 
    } 
 

 
    TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice); 
 
    orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice; 
 
    document.getElementById("Details").value = orderDetails; 
 

 
    } 
 

 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title> Pete's Pizza </title> 
 
</head> 
 

 
<body> 
 
    <h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1> 
 
    <h3> Enter your pizza order: </h3> 
 
    <label> Amount for each standard pizza </label> 
 
    <form name="standard"> 
 
    <input type="text" name="Hawaiian"> Hawaiian Pizza <br> 
 
    <input type="text" name="Cheese"> Cheese Pizza <br> 
 
    <input type="text" name="Veggie"> Veggie Pizza <br> 
 
    <input type="text" name="Supreme"> Supreme Pizza <br> 
 
    <input type="text" name="Pepperoni"> Pepperoni Pizza <br> 
 
    </form> 
 
    <label> Amount for each gourmet pizza </label> 
 
    <form name="gourmet"> 
 
    <input type="text" name="Meatlovers"> Meat-lovers Pizza <br> 
 
    <input type="text" name="Chicken"> Chicken Pizza <br> 
 
    <input type="text" name="Prawn"> Prawn <br> 
 
    </form> 
 
    <form name="getOrderMethod"> 
 
    <input type="radio" name="method" value="Delivery" checked> Delivery <br> 
 
    <input type="radio" name="method" value="Pick-up"> Pick-up <br> 
 
    </form> 
 
    <input type="button" value="Confirm Order" onClick="orderPizzas()"> 
 
    <input type="button" value="Cancel Order" onClick="window.location.reload()"> 
 
    <textarea id="Details" value="" rows="9" cols="33" wrap=on readonly></textarea> 
 
</body> 
 

 
</html>
,

내가 자바 스크립트와 HTML에 아주 새로운 오전, 모든 조언을 잘 받아됩니다. 미리 감사드립니다.

+0

텍스트 영역을 지우는 데 사용하려는 코드는 어디에 있습니까? 우리가 볼 수없는 문제를 해결하도록 도울 수는 없습니다! 또한 [실행 가능한 코드 스 니펫] (https ://)에서 * relevalt * 코드 만 사용하여 [Minimal, Complete, Verifiable example] (https://stackoverflow.com/help/mcve) /stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/) – FluffyKitten

+0

텍스트 영역을 지우는 선을 강조 표시했습니다. 이제 다른 라인이 텍스트 영역의 값을 설정하고 해당 값을 다시 확인하는지 살펴보십시오. 그런데 사용자가 기대하는 형식을 알리지 않고 루프에서 사용자의 전화 번호를 계속 거부하는 것은 매우 짜증나게합니다. – nnnnnn

+0

@nnnnnn 귀하의 의견을 적어 주시고 귀하의 도움과 조언에 감사드립니다! – Stefan

답변

0

var pizzaCount = 0; 
 
    var gourmetPrice = 15.50; 
 
    var standardPrice = 9.50; 
 
    var deliveryCharge = 5; 
 
    var TotalPrice; 
 
    var name; 
 
    var adress; 
 
    var phoneNumber = 10; 
 
    var gourmetCount = 0; 
 
    var standardCount = 0; 
 

 
    function orderPizzas() { 
 
     var customerDetails; 
 
     var orderDetails = ''; 
 
     var i = 0; 
 
     var j = 0; 
 

 
     TotalPrice = 0; 
 
     phoneNumber = ''; 
 

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

 
     var arrStandardPizza = new Array() 
 
     arrStandardPizza[0] = new Array(); 
 
     arrStandardPizza[0]['name'] = 'Hawaiian'; 
 
     arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value); 
 
     arrStandardPizza[1] = new Array(); 
 
     arrStandardPizza[1]['name'] = 'Cheese'; 
 
     arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value); 
 
     arrStandardPizza[2] = new Array(); 
 
     arrStandardPizza[2]['name'] = 'Veggie'; 
 
     arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value); 
 
     arrStandardPizza[3] = new Array(); 
 
     arrStandardPizza[3]['name'] = 'Supreme'; 
 
     arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value); 
 
     arrStandardPizza[4] = new Array(); 
 
     arrStandardPizza[4]['name'] = 'Pepperoni'; 
 
     arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value); 
 

 
     var arrGourmetPizza = new Array() 
 
     arrGourmetPizza[0] = new Array(); 
 
     arrGourmetPizza[0]['name'] = 'Meatlovers'; 
 
     arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value); 
 
     arrGourmetPizza[1] = new Array(); 
 
     arrGourmetPizza[1]['name'] = 'Chicken'; 
 
     arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value); 
 
     arrGourmetPizza[2] = new Array(); 
 
     arrGourmetPizza[2]['name'] = 'Prawn'; 
 
     arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value); 
 

 
     standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount']; 
 
     gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount']; 
 
     pizzaCount = standardCount + gourmetCount; 
 

 
     if (pizzaCount > 12) { 
 

 
      alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount); 
 

 
     } 
 
     else { 
 

 
      while(i < 5) { 
 

 
       if (arrStandardPizza[i]['amount'] > 0) { 
 

 
        orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount']; 
 

 
       } 
 

 
       i++; 
 

 
      } 
 
      while(j < 3) { 
 

 
       if (arrGourmetPizza[j]['amount'] > 0) { 
 

 
        orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount']; 
 

 
       } 
 

 
       j++; 
 

 
      } 
 

 
      if (document.getOrderMethod.method.value == 'Delivery') { 
 

 
       name = prompt('What is your name?'); 
 
       adress = prompt('What is your adress?'); 
 

 
       while(phoneNumber.toString().length !== 10) { 
 

 
        phoneNumber = prompt('What is your phone number?'); 
 

 
       } 
 
       customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber; 
 

 
       TotalPrice = deliveryCharge; 
 

 
      } 
 
      else { 
 

 
       name = prompt('What is your name?'); 
 
       customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name; 
 

 
      } 
 

 
      TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice); 
 
      
 
      orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice; 
 
      document.getElementById("Details").value = orderDetails; 
 

 
     } 
 

 
    }
<h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1> 
 

 
    <h3> Enter your pizza order: </h3> 
 

 
    <label> Amount for each standard pizza </label> 
 

 
    <form name ="standard"> 
 
     <input type="text" name="Hawaiian" > Hawaiian Pizza <br> 
 

 
     <input type="text" name="Cheese" > Cheese Pizza <br> 
 

 
     <input type="text" name="Veggie" > Veggie Pizza <br> 
 

 
     <input type="text" name="Supreme" > Supreme Pizza <br> 
 

 
     <input type="text" name="Pepperoni" > Pepperoni Pizza <br> 
 

 
    </form> 
 

 
    <label> Amount for each gourmet pizza </label> 
 

 
    <form name ="gourmet"> 
 

 
     <input type="text" name="Meatlovers" > Meat-lovers Pizza <br> 
 

 
     <input type="text" name="Chicken" > Chicken Pizza <br> 
 

 
     <input type="text" name="Prawn" > Prawn <br> 
 

 
    </form> 
 

 
    <form name="getOrderMethod"> 
 

 
     <input type="radio" name="method" value="Delivery" checked> Delivery <br> 
 

 
     <input type="radio" name="method" value="Pick-up"> Pick-up <br> 
 

 
    </form> 
 

 
    <input type="button" value="Confirm Order" onClick="orderPizzas()" > 
 

 
    <input type="button" value="Cancel Order" onClick="window.location.reload()" > 
 

 
    <textarea id="Details" value="" rows="9" cols="33" wrap=on readonly> 
 
    </textarea>

CustomerDetails는 함수가 호출 될 때마다 초기화되어야한다.

사용해보기!

+2

이것에 대해 다른 점은 무엇입니까? 우리는 당신이 변경 한 것을보기 위해 그것을 OP의 코드와 라인별로 비교하기로되어 있습니까? 답을 [편집]하여 문제가 무엇이며 어떻게 수정했는지 설명하십시오. – nnnnnn

+0

이 줄은 :'orderDetails = customerDetails + '\ n'+ 'Total Cost : $'+ TotalPrice;' –

+0

다시 말하지만, 답안에서 답을 명확하게 말하면서 답을 편집하십시오. 또한, * line을 바꾸면 피자의 내용이 출력되지 않으므로 OP가 원하는 것이 아닙니다. – nnnnnn

2

orderDetails을 메서드 외부에 정의했기 때문에 문제가 발생한 것으로 보입니다. 매번 변수를 지우거나 범위를 지정해야합니다. 계속 추가하면됩니다.

글로벌 범위의 변수 선언을 메서드 정의로 옮기면 작동합니다.

+0

대단히 감사합니다. 작동합니다! 너는 천재 야! – Stefan

+0

도움이 되셨다면 기꺼이 도와 드리겠습니다. 답변을 수락 한 것으로 표시하는 것을 잊지 마십시오. –

+0

허용으로 표시하려면 어떻게하나요? – Stefan

0

주문을 지우고 있지 않습니다.

orderDetails=""; 

"orderPizzas"기능을 위 코드에서 사용하십시오.

+0

나는 나의 질문을 편집했다. 거기서 textarea의 값을 '초기화'하려고 시도했다. 그렇지 않으면 결과는 출력의 맨 위에 'undefined'와 함께 아직 정확할 것이다. – Stefan