A-A+

jquery和javascript中select的用法介绍

2016年01月18日 前端设计 暂无评论 阅读 5 views 次

javascript用法,代码如下:

  1. var obj=document.getElementById(selectid);  
  2. obj.options.length = 0; //清除所有内容  
  3. obj.options[index] = new Option("three",3);  //更改对应的值  
  4. obj.options[index].selected = true;  //保持选中状态  
  5. obj.add(new Option("4","4")); ”文本",”值"  
  6. var index = obj.selectedIndex;obj.options.remove(index);//删除选中项  

其它,getElementsByTagName的用法,例子,代码如下:

  1. var temp=document.getElementsByTagName("input");   
  2.     for (i = 0; i < temp.length; i++) {   
  3.         if (temp[i].id != "" && temp[i].id!=null) {   
  4.             document.getElementById(temp[i].id).value = "";   
  5.         }   
  6.     }  

jquery,代码如下:

  1. $("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)  
  2. $("#select_id").").find('option:selected').text(); 获取select选中的text 
  3. $("#select_id").val(); 获取select选中的value 
  4. $("#select_id option[index='0']").remove();//删除索引值为0的Option 
  5. $("#select_id option[value='3']").remove(); //删除值为3的Option 
  6. $("#select_id option").remove(); //删除TEXT值为4的Option 
  7. $("#mselect_id").change(function(){     //添加所需要执行的操作代码})  
标签:

给我留言