form表单select的选项值选择

html:

<form action="">
<p>选择城市</p>
<p>
<select name="" id="cont">
<option value="北京" order="1">北京</option>
<option value="上海" order="2">上海</option>
<option value="广州" order="3">广州</option>
</select>
</p>
<p>
<input type="button" value="ok" id="btn"/>
</p>
</form>

js:

 //方法一:取出想要的选项内容
var btn=document.getElementById("btn");
var cont=document.getElementById("cont");
btn.onclick=function(){
//alert(cont.value);
};
//方法二:取出想要的内容
btn.onclick=function(){
//先得到选择的索引值
var index=cont.selectedIndex;
//把option选项放到一个数组里
var arr=cont.options;
//从数组中取出某个索引的值
var text=arr[index].value;
var order=arr[index].getAttribute("order");
alert(order);
};
原文地址:https://www.cnblogs.com/wanan-01/p/7592015.html