如何在javascript中获取select下拉列表的值?
如何在Javascript中从选择下拉框中警告用户选择的值。在本例中,我希望将值存储在变量中,并向用户发出警报如何在javascript中获取select下拉列表的值?,javascript,Javascript,如何在Javascript中从选择下拉框中警告用户选择的值。在本例中,我希望将值存储在变量中,并向用户发出警报 <script type="text/javascript"> function processmyform() { var chosenAnimal = // what goes here alert(chosenAnimal); } </script> <form action=""> <select na
<script type="text/javascript">
function processmyform() {
var chosenAnimal = // what goes here
alert(chosenAnimal);
}
</script>
<form action="">
<select name="animal" class="favAnimal">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
<a href="#" onclick="processmyform()" />
</form>
函数processmyform(){
var chosenAnimal=//这里是什么
警惕(乔森动物);
}
狗
猫
鸟
我通常会在select上添加一个id,比如id=“animal”
然后你可以做:
var animal = document.getElementById("animal");
var animalValue = animal.options[animal.selectedIndex].value;
alert(animalValue);
我通常会在select上添加一个id,比如
id=“animal”
然后你可以做:
var animal = document.getElementById("animal");
var animalValue = animal.options[animal.selectedIndex].value;
alert(animalValue);
我会在您的选择标签上设置一个id:
<select id="animal" name="animal" class="favAnimal">
我会在您的选择标签上设置一个id:
<select id="animal" name="animal" class="favAnimal">
使用
selectedIndex
<form id="yourForm" action="#">
<select id="animal" name="animal" class="favAnimal">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
<input type="submit" value="Submit" />
</form>
window.onload = function(){
var selectElement = document.getElementById('animal');
document.getElementById('yourForm').onsubmit = function(){
var index = selectElement.selectedIndex;
alert(selectElement.options[index].value);
alert(selectElement.options[index].text);
alert(index);
return false;
};
};
狗
猫
鸟
window.onload=函数(){
var selectElement=document.getElementById('animal');
document.getElementById('yourForm')。onsubmit=function(){
var index=selectElement.selectedIndex;
警报(selectElement.options[index].value);
警报(selectElement.options[index].text);
警报(索引);
返回false;
};
};
使用selectedIndex
<form id="yourForm" action="#">
<select id="animal" name="animal" class="favAnimal">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
<input type="submit" value="Submit" />
</form>
window.onload = function(){
var selectElement = document.getElementById('animal');
document.getElementById('yourForm').onsubmit = function(){
var index = selectElement.selectedIndex;
alert(selectElement.options[index].value);
alert(selectElement.options[index].text);
alert(index);
return false;
};
};
狗
猫
鸟
window.onload=函数(){
var selectElement=document.getElementById('animal');
document.getElementById('yourForm')。onsubmit=function(){
var index=selectElement.selectedIndex;
警报(selectElement.options[index].value);
警报(selectElement.options[index].text);
警报(索引);
返回false;
};
};
首先,如果您的选择框有一个id,这会更容易。然后您可以使用getElementById
var animalSelectBox = document.getElementById('animal');
alert(animalSelectBox.options[animalSelectBox.selectedIndex].value);
相反,下面是如何使用getElementsByName
(注意这是复数形式)
首先,如果您的选择框有一个id,那么会更容易。然后您可以使用
getElementById
var animalSelectBox = document.getElementById('animal');
alert(animalSelectBox.options[animalSelectBox.selectedIndex].value);
相反,下面是如何使用getElementsByName
(注意这是复数形式)
您使用的是像jQuery这样的JavaScript框架吗?您使用的是像jQuery这样的JavaScript框架吗?不过,请小心,getElementsByName在IE和Opera中具有非标准行为(获取与名称或id匹配的元素),并且不会对IE中的某些元素起作用。(请参阅)在本例中应该可以正常工作(尽管大家似乎都同意,向select添加一个id会更好)但是要小心,getElementsByName在IE和Opera中有非标准行为(获取与name或id匹配的元素),并且不会在IE中的某些元素上工作。(请参阅)在本例中应该可以正常工作(尽管向select添加id会更好,因为每个人似乎都同意这一点)