2012-10-11 144 views
2

我有一個工作稅計算器,我需要再添加一件。對於輸入C,我需要有兩個與用戶選擇的城鎮相關的已知值。然後,我需要將這些值插入計算函數的不同位置,但Calculate2中只有一個值位於一個位置。就像:將選項選擇的兩個值傳遞給JavaScript函數

<select name="input_C" id="input_C"> 
<option value="value1,value2">Acworth</option> 
</select> 

function Calculate(Atext, Btext, Ctext_value1, Ctext_value2, form) 
{ 
    var A = parseFloat(Atext); 
    var B = parseFloat(Btext); 
    var C_1 = parseFloat(Ctext_value1); 
    var C_2 = parseFloat(Ctext_value2); 
    form.Answer.value = ((B/C_1)-A)*C_2; 
} 

function Calculate2(Atext, Ctext_value2, Dtext, form) 
{ 
    var A = parseFloat(Atext); 
    var C_2 = parseFloat(Ctext_value2); 
    var D = parseFloat(Dtext); 
    form.Answer.value = D-((A*C_2)/1000); 
} 

但我不知道如何傳遞這兩個值並將它們正確關聯?

下面的代碼有效,但顯然缺少與input_C相關的兩個值。我敢肯定,我需要別的東西添加到document.getElementById("input_C").value,除了其他變化......

function compute() { 
    var A = document.getElementById("input_A").value; 
    var B = document.getElementById("input_B").value; 
    var C = document.getElementById("input_C").value; 
    var D = document.getElementById("input_D").value; 

    if (B != "" && D != "") { 
     alert("You may only enter either an Assessment or Annual Taxes."); 
    } else if (A =="" && B =="" && D =="") { 
     alert("You must select your town, enter a Market Value, and either an Assessment or  Annual Taxes to calcuate.."); 
    } else if (C =="999") { 
     alert("You must select your town."); 
    } else if (A =="") { 
     alert("You must enter a Market Value."); 
    } else if (B != "" && C !="") { 
     Calculate(A, B, C, document.getElementById("overtax")); 
    } else if (D != "" && C !="") { 
     Calculate2(A, C, D, document.getElementById("overtax")); 
    } else { 
     alert("You must select your town, enter a Market Value, and either an Assessment or  Annual Taxes to calcuate."); 
    } 
} 

function Calculate(Atext, Btext, Ctext, form) 
{ 
    var A = parseFloat(Atext); 
    var B = parseFloat(Btext); 
    var C = parseFloat(Ctext); 
    form.Answer.value = ((B-A)*C)/1000; 
} 

function Calculate2(Atext, Ctext, Dtext, form) 
{ 
    var A = parseFloat(Atext); 
    var C = parseFloat(Ctext); 
    var D = parseFloat(Dtext); 
    form.Answer.value = D-((A*C)/1000); 
} 


<FORM NAME="Calculator" METHOD="post" id="overtax"> 
    <P>City/Town: 
     <select name="input_C" id="input_C"> 
      <option value="999">Select Your City/Town</option> 
      <option value="20.55">Acworth</option> 
     </select> 
    </P> 
    <P>Market Value: <INPUT TYPE=TEXT NAME="input_A" id="input_A" SIZE=10  onblur="this.value=this.value.replace(/,/g,'')"> <span class="directions">Please enter what you believe your property is worth in today's market</span></P> 
    <P>Assessment: <INPUT TYPE=TEXT NAME="input_B" id="input_B" SIZE=10 onblur="this.value=this.value.replace(/,/g,'')"> <span class="directions">Located on your property tax bill or assessment card</span></P> 
    <P>OR</P> 
    <P>Annual Taxes: <INPUT TYPE=TEXT NAME="input_D" id="input_D" SIZE=10 onblur="this.value=this.value.replace(/,/g,'')"> <span class="directions">Located on your property tax bill or assessment card</span></P> 

    <P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onclick="compute();" /></P> 
+0

你的問題,似乎都不清楚,你可能想通過使用澄清在jsfiddle.net的一半工作示例或其他東西,然後共享鏈接。 – Rayshawn

回答

5

如果我理解正確,你只需要拆分值的字符串

例如鑑於此HTML

<select id="mySelect"> 
    <option value="100,200">My Value</option> 
</select>​ 

那麼這會給你的價值觀

var value = document.getElementById("mySelect").value; 
var split = value.split(","); 
var v1 = split[0]; 
var v2 = split[1]; 

例如看到jsfiddle

更多的split方法here

+0

謝謝 - 這個伎倆!我不知道使用什麼搜索詞(split)來找到答案。 – Lauren

1

我們可以用數據屬性傳遞,並可在JS訪問,

<OPTION VALUE="1" data-year="2010">One</OPTION> 

var year = $(this).find(':selected').data('year'); 
相關問題