当前位置: 代码迷 >> Web前端 >> jquery select option 设立selected属性
  详细解决方案

jquery select option 设立selected属性

热度:696   发布时间:2013-08-10 21:14:06.0
jquery select option 设置selected属性
//jquery选中select
function selectByValue(sltName,value){   
	$("select[@name="+sltName+"] option").each(function(){    
	if($(this).val() == value){    
	$(this).attr("selected","selected");    
	}    
	});   
} 

//更直接:
$("#sltName").val(data.topicVoteBean.maxCount); 


取值:
【引自:http://zhangyulong.iteye.com/blog/1328615】
var sex  = $("#search_form select[name='sex'] option:selected ").attr("value");

var sex = $("#search_form select[name=sex'] option[selected]").attr("value");
这样写会有问题 sex的值有时是undifined 
改为
var sex  = $("#search_form select[name='sex'] option:selected ").attr("value");

var val = $("#search_form select[name='sex']").children('option:selected').val();
这样就去到了
  相关解决方案