2016-01-21 34 views
2

這是fiddle,所以你可以看到我在說什麼。如何使JavaScript寫入價格基於輸入?

基本上這是一個價格檢查器,我需要使用Javascript才能顯示一些值。

所以你可以看到我有div .offer,我有一個行動呼籲的提議。當用戶加載頁面時,div將顯示「Please select a country」

按下「檢查價格」後,將顯示一個段落,其中包含一個價格和其下的黃色按鈕。 所以,我的問題是,我該怎麼做?我如何創建一個腳本來檢查「to」和「from」這兩個值,以顯示適當的價格?例如,如果他從德國選擇到美國,它應該顯示0.02美元/分鐘。如果他從西班牙到意大利選擇,它應該顯示0.05美元/分鐘等。我希望你明白。

感謝提前:)

<div class="offer"> 
    <p>Our special offer for you is:</p> 
    <p id="big-offer">Only $0.01/min</p> 
    <button type="submit" class="btn btn-default btn-lg" id="big-cta">Strong Call to Action!</button> 
</div> 

回答

1

我建立類似前陣子一個客戶的東西,這是醜陋的地獄,絕對可以有很多更幹,但它的作品。我的報價生成器有三個參數:服務,臥室和傢俱,並返回一個值。你應該能夠調整這個以適應你的需求。如果你想看到它的工作,你可以check it out here。這是一個單:

var quoteMaker = { 
     data : { 
      'services':null, 
      'bedrooms':null, 
      'furnishing':null, 
      'quoteString':'' 
     }, 
     servicesListener : function(){ 
      this.data.services = document.getElementById('service').options[document.getElementById('service').selectedIndex].text; 
     }, 
     bedroomsListener : function(){ 
      this.data.bedrooms = document.getElementById('bedrooms').options[document.getElementById('bedrooms').selectedIndex].text; 
     }, 
     furnishingListener : function(){ 
      this.data.furnishing = document.getElementById('furnishing').options[document.getElementById('furnishing').selectedIndex].text; 
     }, 
     changeData : function(divObj){ 
      this.data.quoteString=''; 
      if(divObj.id == 'service'){ 
       this.servicesListener(); 
      }else if(divObj.id == 'bedrooms'){ 
       this.bedroomsListener(); 
      }else if(divObj.id == 'furnishing'){ 
       this.furnishingListener(); 
      } 
      this.updateQuote(); 
     }, 
    updateQuote : function() { 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="1" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £70"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="2" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £75"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="3" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £80"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="4" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £85"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="5" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £90"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="6+" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £100"; 

     if (this.data.services == "Inventory" &&this.data.bedrooms =="1" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="2" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £65"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="3" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £70"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="4" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £75"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="5" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £80"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="6+" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £85"; 

     if (this.data.services == "Inventory" &&this.data.bedrooms =="1" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="2" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="3" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £65"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="4" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £70"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="5" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £75"; 
     if (this.data.services == "Inventory" &&this.data.bedrooms =="6+" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £80"; 


     if (this.data.services == "Check-In" &&this.data.bedrooms =="1" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £50"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="2" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="3" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="4" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £65"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="5" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £70"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="6+" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £75"; 

     if (this.data.services == "Check-In" &&this.data.bedrooms =="1" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £40"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="2" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £45"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="3" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £50"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="4" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="5" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="6+" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £65"; 

     if (this.data.services == "Check-In" &&this.data.bedrooms =="1" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £35"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="2" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £40"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="3" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £45"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="4" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £50"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="5" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-In" &&this.data.bedrooms =="6+" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £60"; 


     if (this.data.services == "Check-Out" &&this.data.bedrooms =="1" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="2" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="3" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £65"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="4" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £70"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="5" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £75"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="6+" && this.data.furnishing == "Furnished") 
     this.data.quoteString = "= £80"; 

     if (this.data.services == "Check-Out" &&this.data.bedrooms =="1" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £45"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="2" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £50"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="3" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="4" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="5" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £65"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="6+" && this.data.furnishing == "Part Furnished") 
     this.data.quoteString = "= £70"; 

     if (this.data.services == "Check-Out" &&this.data.bedrooms =="1" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £40"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="2" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £45"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="3" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £50"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="4" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £55"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="5" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £60"; 
     if (this.data.services == "Check-Out" &&this.data.bedrooms =="6+" && this.data.furnishing == "Unfurnished (except for white goods)") 
     this.data.quoteString = "= £65"; 
    document.getElementById("quote").innerHTML=this.data.quoteString; 
    }, 
    init: function(){} 
} 
quoteMaker.init(); 
+1

謝謝你,艾倫:)。我認爲這正是我需要的。我會檢查出來的。 – Mike

1

好球員,這裏是我是如何做的:

function getPricing() { 
 
    var e = document.getElementById("from"); 
 
    var fromCountry = e.options[e.selectedIndex].text; 
 
    var f = document.getElementById("to"); 
 
    var toCountry = f.options[f.selectedIndex].text; 
 
    var g = fromCountry + toCountry; 
 
    var price = 0; 
 
     
 
    if (g === "GermanyRomania") {price = 0.25;}; 
 
    if (g === "GermanyBulgaria") {price = 0.19;}; 
 
    if (g === "GermanyItaly") {price = 0.32;}; 
 
    if (g === "FranceRomania") {price = 0.98;}; 
 
    if (g === "FranceBulgaria") {price = 0.11;}; 
 
    if (g === "FranceItaly") {price = 0.25;}; 
 
    if (g === "SpainRomania") {price = 0.19;}; 
 
    if (g === "SpainBulgaria") {price = 0.39;}; 
 
    if (g === "SpainItaly") {price = 0.12;}; 
 

 
    
 
    if (fromCountry != "From Country" && toCountry != "To Country") { 
 
     document.getElementById("wholeoffer").innerHTML ='<p>Our special offer for you is:</p><p id="big-offer">Only $<span id="hahaha">0.09</span>/min</p><button type="submit" class="btn btn-default btn-lg" id="big-cta">Strong Call to Action!</button>'; 
 
      
 
    document.getElementById("hahaha").innerHTML = price; 
 
      
 
     
 
     
 
     
 
    } 
 
    else { 
 
    document.getElementById("wholeoffer").innerHTML ="<p>Please keep in mind that you have to choose both countries in order to work</p><button type='submit' class='btn btn-default btn-lg' id='invalid'>Get more details</button> "; 
 
    } 
 

 
    
 
    
 
}