js获得select选中的值 JQuery获得select选中的值
用JQuery写的:
Jquery获得select下拉选项option的值:<script type"text/javascript">$(document).ready(function() {$("#selProv").change(function(){alert( $("#selProv option:selected").text() );})});//alert($("#selProv option[value='01']").val());//也可以这样获得一个值</script><select id="selProv"><option value="1">text111111</option><option value="2">text222222</option></select>
?
用javascript的写的:
var str = document.getElementById("stBegin"); alert(str.options[str.selectedIndex].value);
?
如何添加select的option呢?
有如下两种方法,当然还有其它的方法,下面的只是经常用到:
①JS代码:
var op = new Option("text", "value");//创建一个option对象op.selected = true;//设置其为默认选中项$("#stBegin")[0].options.add(op);//添加到select对像
?②JQuery代码:
$("#stBegin").append("<option value='00000' selected>oooo</option>");//使用JQuery的append方法,来天添加
?
?
?