查看文章
 
jQuery操作select
2010-05-06 13:37
  1. //获取第一个option的值  
  2. $('#test option:first').val();  
  3. //最后一个option的值   
  4. $('#test option:last').val();  
  5. //获取第二个option的值  
  6. $('#test option:eq(1)').val();  
  7. //获取选中的值  
  8. $('#test').val();  
  9. $('#test option:selected').val();  
  10. //设置值为2的option为选中状态  
  11. $('#test').attr('value','2');  
  12. //设置第一个option为选中  
  13. $('#test option:last').attr('selected','selected');  
  14. $("#test").attr('value' , $('#test option:last').val());  
  15. $("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());  
  16. //获取select的长度  
  17. $('#test option').length;  
  18. //添加一个option  
  19. $("#test").append("<option value='9'>ff</option>");  
  20. $("<option value='9'>ff</option>").appendTo("#test");  
  21. //添除选中项  
  22. $('#test option:selected').remove();  
  23. //指定项选中  
  24. $('#test option:first').remove();  
  25. //指定值被删除  
  26. $('#test option').each(function(){  
  27.     if( $(this).val() == '5'){  
  28.          $(this).remove();  
  29.      }  
  30. });  
  31. $('#test option[value=5]').remove();  
  32.   
  33. //获取第一个Group的标签  
  34. $('#test optgroup:eq(0)').attr('label');  
  35. //获取第二group下面第一个option的值   
  36. $('#test optgroup:eq(1) :option:eq(0)').val();  

类别:Javascript||添加到搜藏 |分享到i贴吧|浏览(346)|评论 (0)
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu