14.select框的选中jquery
误区:ajax
一直觉得jquery获取select中option被选中的文本值,是这样写的:async
$("#s").text(); //获取全部option的文本值spa
实际上应该这样:code
$("#s option:selected").text(); //获取选中的option的文本值索引
获取select中option的被选中的value值,rem
$("#s").val();get
$("#s option:selected").val();io
让select中value为某一值的option被选中select
$("#s").find("option[value='xx']").attr("selected",true);
若是没有生效则换成:
$('#s').val(saveValue).trigger('change');
若是是用ajax动态添加option的话,以上的方法也会行不通,把async设置成false就能够了。
其中saveValue为value值。
js获取select选中的值:
var sel=document.getElementById("select1");
var index = sel.selectedIndex; // 选中索引
albumid= sel.options[index].value;//要的值
删除select下的option:
$("#s").options.length=0; $("#s").options.remove(index),index为必须
获取select下option的个数:$("#s")[0].options.length