我寫的附加一個onchange
事件給每個窗體元素和變化更新this.data值的單例。從看你的代碼和你的jsfiddle(這對我不起作用),這種方法可能沒有多大意義。我已經在我的本地機器上測試過它,它可以正常工作
<script>
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();
</script>
<div id="form">
<p class="lead">Complete the form for an instant quote</p>
<form name="quote-maker" action="">
<p>Select service<br />
<select id="service" name="services" onchange="quoteMaker.changeData(this);">
<option value="Inventory">Inventory</option>
<option value="Check-In">Check-In</option>
<option value="Check-Out">Check-Out</option>
</select></p>
<p class="lead">How many bedrooms does the property have?<br />
<select id="bedrooms" name="bedrooms" onchange="quoteMaker.changeData(this);">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="5">6+</option>
</select></p>
<p class="lead">Is the property furnished or unfurnished?<br />
<select id="furnishing" name="furnishing" onchange="quoteMaker.changeData(this);">
<option value="1">Furnished</option>
<option value="2">Part Furnished </option>
<option value="3">Unfurnished (except for white goods)</option>
</select></p>
</form>
</div>
<div id="quote"></div>
注意:我清理了您的html。將所有的<select>
輸入封裝在一個表單中
你想讓返回的報價看起來像什麼? ##£作爲一個字符串?當我聽到'報價'時,我想總計但我猜這不是你要找的 –
是的我想結果是一個字符串,即「£75」,我想出現在「=」在表格下方的div中。我希望將三個選定的選項作爲其三個參數傳遞給該函數,並且當找到匹配的三個選項時,將返回適當的字符串,例如「£75」。我是否需要在此函數之上構建函數,或者向數組發送三個選項,然後將數組傳遞給函數?如果在點擊時返回報價單的表單下方有一個提交按鈕,我會同樣高興。有沒有更簡單的方式來編寫我的功能?謝謝 – Pixelomo
你可以把我的代碼放在下面,並添加一些智能來設置你的字符串值嗎?或者你需要幫助嗎? –