Java脚本中HTML选择控件的访问值和所选文本
问题描述:
我有一个这样的选择控件.
I have a select control like this.
<script type="text/javascript">
function countrychange() {
var cou = document.getElementById('country').value;
alert(cou);
}
</script>
<select id="country" name="D1" önchange="countrychange()">
<option value="IN">India</option>
<option value="US">USA</option>
</select>
当我更改任何国家/地区时,它将显示我的价值,但是我想这样显示,假设选择了Option-India,那么它应该向我显示印度.
When I change any country then it is displaying me it''s value, but I want to display like this, suppose Option-India selected, then it should show me India.
答
您是否熟悉Jquery ..?
或尝试这个
are you familiar with Jquery..?
or Try this
var name=document.getElementById('country')[document.getElementById('country').nodeType].innerHTML
var sel = document.getElementById('country');
var index = sel.selectedIndex;
var text = sel.options(index).text;
alert('the text is :'+ text + '\nthe value is : '+sel.value);