2013-04-10 1 views
0

모든 정보를 입력 한 후 호텔 청구서와 관련된 학교 프로젝트가 있습니다. 나는 아래 function displayBill() 자바 스크립트에서 "할인 : $ 20"을 표시하는 경우에만 버그 할인은 HTML 양식에서 예로 선택하면. 누군가가 양식에서 no 옵션을 선택하면 javascript 함수는 bugDiscount = 0으로 표시되지만 새 페이지 (청구 요약)에 할인을 표시하지 않으려합니다. 나는 그것이 나타나는 이유는 displayBill() 함수가 호출되었을 때 항상 할인을 보여주기 때문이다. 나는 2 가지 다른 displayBill()을 만들어야합니까? 아니면 더 쉬운 방법이 있을까요?Javascript에서 newPage의 출력을 표시하는 HTML 라디오

다음
<form> 
    Guest ID Number: <input type="text" id="custID" /><br /> 
    Room Type: 
    <select id="roomType" /> 
     <option></option> 
     <option>Double</option> 
     <option>Single</option> 
     <option>Parlor</option> 
    </select><br /> 
    Length of Stay: <input type="text" id="stayLength" name="numOranges" /><br /> 
    Number of Drinks: <input type="text" id="drinkNum" name="numOranges" /><br /> 
    Number of Towels: <input type="text" id="towelNum" name="numOranges" /><br /> 
    Number of Flushes: <input type="text" id="flushNum" name="numOranges" /><br /> 
    Bug Complaints?: <br /> <label> 
     <input type="radio" name="bugComplaint" value="Yes" onclick="getCheckedRadio(this)" />Yes<br /> 
     <input type="radio" name="bugComplaint" value="No" onclick="getCheckedRadio(this)" />No<br /> 
     <br /> 
    Customer Comments: <br /> 
    <textarea name="customerComment" cols="50" rows="5">Enter your comments here...</textarea> <br /> 
    <br /> 

    <input type="button" onclick="calculateFinalBill()" value="Calculate"> 
</form> 

는 자바 스크립트입니다 : 여기

는 HTML 코드의

const doublePrice = 150; 
const singlePrice = 100; 
const parlorPrice = 80; 
const drinkPrice = 5; 
const towelPrice = 3; 
const flushPrice = 1; 

var custID; 
var roomPrice; 
var stayLength; 
var drinkNum; 
var towelNum; 
var flushNum; 
var totalDue; 
var subtotal; 
var roomType; 
var bugDiscount; 

function calculateFinalBill() { 
validateForm(); 
    if(roomType == "Double"){ 
    roomPrice = doublePrice; 
    } 

    if(roomType == "Single"){ 
    roomPrice = singlePrice; 
    } 

    if(roomType == "Parlor"){ 
    roomPrice = parlorPrice; 
    } 

    roomTotal = roomPrice * stayLength 

    towelTotal = towelPrice * towelNum 

    flushTotal = flushPrice * flushNum 

    drinkTotal = drinkPrice * drinkNum 

    subtotal = roomTotal + towelTotal + flushTotal + drinkTotal 

    totalDue = subtotal - bugDiscount 

    displayBill(); 
} 

function getCheckedRadio(which){ 
var bugValue = which.value; 
    if (bugValue == "No"){ 
    bugDiscount = 0; 
    } 
    if (bugValue == "Yes"){ 
    bugDiscount = 20; 
    } 
} 

function validateForm(){ 
custID = parseInt(document.getElementById("custID").value); 
if(isNaN(custID)){ 
    alert('Customer ID must be a number'); 
    return; 
} 
if(custID <= 0){ 
    alert('Customer ID must be greater than zero'); 
    return; 
} 

roomType = document.getElementById("roomType").value; 
if(roomType == ""){ 
    alert("Room type must be selected"); 
    return; 
} 

stayLength = parseInt(document.getElementById("stayLength").value); 
if(isNaN(stayLength)){ 
    alert('Length of Stay must be a number'); 
    } 
if(stayLength < 0){ 
    alert('Length of Stay must be greater than zero'); 
    return; 
} 

drinkNum = parseInt(document.getElementById("drinkNum").value); 
if(isNaN(drinkNum)){ 
    alert('Number of Drinks must be a number'); 
    } 
if(drinkNum < 0 || drinkNum > 25){ 
    alert('Number of Drinks must be 0-25'); 
    return; 
} 

towelNum = parseInt(document.getElementById("towelNum").value); 
if(isNaN(towelNum)){ 
    alert('Number of Towels must be a number'); 
    } 
if(towelNum < 0){ 
    alert('Number of Towels must be zero or greater'); 
    return; 
} 

flushNum = parseInt(document.getElementById("flushNum").value); 
if(isNaN(flushNum)){ 
    alert('Number of Flushes must be a number'); 
    } 
if(flushNum < 0){ 
    alert('Number of Flushes must be zero or greater'); 
    return; 
} 

customerComment = document.getElementById("customerComment"); 
} 

function displayBill(){ 
var newPage = "<html><head><title>Billing Summary</title></head>"; //Add CSS after title 
    newPage += "<body><h1>Happy Hoppin Hotel</h1>"; 
    newPage += "<h2>Guest Billing Summary</h2>"; 
    newPage += "Guest Identification: " + custID; 
    newPage += "<br />"; 
    newPage += "Room Type: " + roomType; 
    newPage += "<br />"; 
    newPage += "Length of Stay: " + stayLength; 
    newPage += "<br />"; 
    newPage += "Room Charge: $" + roomTotal; 
    newPage += "<br />"; 
    newPage += "Drink Charge: $" + drinkTotal; 
    newPage += "<br />"; 
    newPage += "Towel Charge: $" + towelTotal; 
    newPage += "<br />"; 
    newPage += "Flushing Charge: $" + flushTotal; 
    newPage += "<br />"; 
    newPage += "Subtotal: $" + subtotal; 
    newPage += "<br />"; 
    newPage += "Discount: $" + bugDiscount; 
    newPage += "<br />"; 
    newPage += "Total Due: $" + totalDue; 
    newPage += "<br />"; 

var j = window.open('','','width=400,height=500'); 
j.document.write(newPage); 
j.document.close(); 
} 

이 새 페이지 (청구 요약) 버그 할인 http://i.imgur.com/2ipm4LL.jpg

에게있을 때 어떻게 보일지입니다

버그 할인이없는 경우 표시되는 내용입니다. http://i.imgur.com/amYl1fu.jpg

제 질문에 혼란이 있다면 명확히 설명해주십시오. 혼란스럽게 만들지 않고이 질문을하는 방법을 생각하는 데 어려움을 겪고있었습니다.

답변

1

귀하의 질문은 실제로 당신이 자바 스크립트에서 모든 이유가 명확하지 않기 때문에 확실히 명확하지 않습니다. 어쨌든, 당신이 할 수있는 일은 bugDiscount 값을 테스트하는 것입니다 :

function displayBill(){ 
var newPage = "<html><head><title>Billing Summary</title></head>"; //Add CSS after title 
    newPage += "<body><h1>Happy Hoppin Hotel</h1>"; 
    newPage += "<h2>Guest Billing Summary</h2>"; 
    newPage += "Guest Identification: " + custID; 
    newPage += "<br />"; 
    newPage += "Room Type: " + roomType; 
    newPage += "<br />"; 
    newPage += "Length of Stay: " + stayLength; 
    newPage += "<br />"; 
    newPage += "Room Charge: $" + roomTotal; 
    newPage += "<br />"; 
    newPage += "Drink Charge: $" + drinkTotal; 
    newPage += "<br />"; 
    newPage += "Towel Charge: $" + towelTotal; 
    newPage += "<br />"; 
    newPage += "Flushing Charge: $" + flushTotal; 
    newPage += "<br />"; 
    newPage += "Subtotal: $" + subtotal; 
    newPage += "<br />"; 
    if(bugdiscount != 0) 
    { 
     newPage += "Discount: $" + bugDiscount; 
     newPage += "<br />"; 
    } 
    newPage += "Total Due: $" + totalDue; 
    newPage += "<br />"; 

var j = window.open('','','width=400,height=500'); 
j.document.write(newPage); 
j.document.close(); 
} 
+0

할인이 필요하지 않을 때 할인을 표시하고 싶지 않습니다. 그래서 나는 그 반대가 필요합니다. 나는 새로운 페이지를위한 Discount 라인을 무효화하는 방법을 이해할 수 없다. – TheUnCola

+0

적어도 이것을 시도해 보셨습니까? bugdiscount가 0이 아닌 이상 할인을 표시하지 않습니다 ... 정확히 찾고있는 것 같습니다 ... 어쨌든 정확한 반대가 필요한 경우 "! ="를 "=="로 변경하십시오. 조건과 그게 다야 ... –

+0

죄송합니다, 나는 아주 새로운 javascript입니다. 나는 그게 뭔가 다른 생각이지만 당신 말이 맞아. 나는 그것을 어지럽히고 내가 일을하고 대답을 받아 들일 수 있는지 알아 보겠습니다. – TheUnCola

관련 문제