[英]How do I get the value of an item in a drop down menu list using javascript?
Basically, I am making a simple tax calculator and I need to get the value of an element within a drop down menu (the specific state) and then based on that, add that state's income tax rate to a variable to be used for future calculation. 基本上,我正在制作一个简单的税计算器,我需要在下拉菜单(特定状态)中获取元素的值,然后基于该值,将该州的所得税率添加到变量中,以用于将来的计算。
Here's my HTML code: 这是我的HTML代码:
<fieldset id="taxinfo">
<label for="state">State of residence</label>
<select id="state" name="state">
</select>
</fieldset>
I have populated it with this javascript code: 我用以下javascript代码填充了它:
var stateList = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV",
"NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
// array to add months to drop-down list menu
function addStates() {
var select = document.getElementById("state");
for (var i = 0; i < stateList.length; i++) {
var list = stateList[i];
var el = document.createElement("option");
el.innerHTML = list;
el.value = list;
select.appendChild(el);
}
}
For example, if someone chose "TX" from the drop down menu list, I would want to set a variable named stateTax to 0; 例如,如果有人从下拉菜单列表中选择“ TX”,我想将一个名为stateTax的变量设置为0;
Edit: couldn't get Gregg's answer to work for me. 编辑:无法得到格雷格的答案为我工作。 Tried to use it in an if/else decision structure as such --- doesn't seem to be working: 试图在if / else决策结构中使用它---似乎不起作用:
// function to acquire tax rate based on state selected from menu list
function getStateTax() {
var stateTax;
var sel = document.getElementById("state");
var st = sel.options[sel.selectedIndex].text;
// if/else decision structure to set appropriate tax based on state
if (st == "TX") {
stateTax = 0;
}
}
The plain javascript way: 普通的javascript方式:
var sel = document.getElementById("state");
var state = sel.options[sel.selectedIndex].value;
the jquery way jQuery的方式
var state = $('#state').val();
Edit function for state tax: 编辑州税功能:
I would convert your state list to a javascript object to hold the tax rates for each state like this: 我会将您的州列表转换为javascript对象,以保存每个州的税率,如下所示:
var taxRate = { 'AL': .07, 'AK': .05, 'AZ': .07 }
Then you can create your state list easily from the javascript object like this: 然后,您可以像这样从javascript对象轻松创建状态列表:
var stateList = Object.keys(taxRate);
You can use your addStates function just like it is: 您可以像使用它一样使用addStates函数:
function addStates() {
var select = document.getElementById("state");
for (var i = 0; i < stateList.length; i++) {
var list = stateList[i];
var el = document.createElement("option");
el.innerHTML = list;
el.value = list;
select.appendChild(el);
}
}
And get the Tax Rate like this: 并像这样获得税率:
function getStateTax(){
var sel = document.getElementById("state");
var state = sel.options[sel.selectedIndex].value;
return taxRate[state];
}
See this fiddle: http://jsfiddle.net/3fqv2jwa/1/ 看到这个小提琴: http : //jsfiddle.net/3fqv2jwa/1/
Here's the same thing only in a jQuery version: http://jsfiddle.net/3fqv2jwa/2/ 这仅在jQuery版本中是相同的: http : //jsfiddle.net/3fqv2jwa/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.