Jquery操作select标签

本文固定链接:http://www.verydemo.com/demo_c110_i17835.html

1.判断select选项中 是否存在Value="paraValue"的Item
$("#selectid option[value='paraValue']").length>0
2.向select选项中 加入一个Item
$("#selectid").append("<option value=''>1111<option>");
3.从select选项中 删除一个Item
$("#selectid").remove("<option value=''>1111<option>");
4.修改select选项中 value="paraValue"的text为"paraText"
$("#selectid option:selected").attr("value","paraValue").attr("text","paraText");
5. 设置select中text="paraText"的第一个Item为选中
$("#selectid option[text='paraText']").attr("selected","true")
6.设置select中 value="paraValue"的Item为选中
$("#selectid option[value='paraValue']").attr("selected","true")

....................................................................

8. 得到select的当前选中项的value
$("#selectid").val();
9.得到select的当前选中项的text
$("#selectid").text();
10. 得到select的当前选中项的Index
document.getElementById("select1").selectedIndex;
$("#selectid").get(0).selectedIndex
11. 清空select的项
$("#selectid").empty();

JS版本的:


 jsSelectIsExitItem(objSelect,objItemValue)   
{   
      isExit =  ;   
      (  i=0;i;I++)   
     {   
          (objSelect.options[i].value == objItemValue)   
         {   
             isExit =  ;   
              ;   
         }   
     }        
      isExit;   
}   
  

 jsAddItemToSelect(objSelect,objItemText,objItemValue, objItemPos)   
{   
     
      (jsSelectIsExitItem(objSelect,objItemValue))   
     {   
         alert(  );   
     }   
        
     {   
          varItem =  Option(objItemText,objItemValue);   

         objSelect.options.add(varItem, objItemPos);   
         alert(  );   
     }      
}   
  

 jsRemoveItemFromSelect(objSelect,objItemValue)   
{   
     
      (jsSelectIsExitItem(objSelect,objItemValue))   
     {   
          (  i=0;i;I++)   
         {   
              (objSelect.options[i].value == objItemValue)   
             {   
                 objSelect.options.remove(i);   
                  ;   
             }   
         }          
         alert(  );              
     }   
        
     {   
         alert(  );   
     }      
}   
  

 jsUpdateItemToSelect(objSelect,objItemText,objItemValue)   
{   
     
      (jsSelectIsExitItem(objSelect,objItemValue))   
     {   
          (  i=0;i;I++)   
         {   
              (objSelect.options[i].value == objItemValue)   
             {   
                 objSelect.options[i].text = objItemText;   
                  ;   
             }   
         }          
         alert(  );              
     }   
        
     {   
         alert(  );   
     }      
}   
          

 jsSelectItemByValue(objSelect,objItemText)   
{      
     
      isExit =  ;   
      (  i=0;i;I++)   
     {   
          (objSelect.options[i].text == objItemText)   
         {   
             objSelect.options[i].selected =  ;   
             isExit =  ;   
              ;   
         }   
     }        
     
      (isExit)   
     {   
         alert(  );              
     }   
        
     {   
         alert(  );   
     }      
}   
  

document.all.objSelect.value = objItemValue;   
  

 currSelectValue = document.all.objSelect.value;   
  

 currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text;   
  

 currSelectIndex = document.all.objSelect.selectedIndex;   
  

document.all.objSelect.options.length = 0;  

猜你喜欢

转载自a74682246679.iteye.com/blog/2073696