JQuery對select的操作

來源:互聯網
上載者:User

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" media="all" type="text/css" href="/css/screen.css" />
<link rel="stylesheet" media="all" type="text/css" href="/css/examples.css" />  
<script type="text/javascript" src="<%=path %>/js/jquery/jquery.js"></script>
<script language="javascript" src="<%=path %>/js/jquery/jquery.select.js"></script>   

<script type="text/javascript" src="<%=path %>/js/jquery/effects.core.js"></script>
<script type="text/javascript" src="<%=path %>/js/jquery/effects.highlight.js"></script>
<script type="text/javascript" src="/js/jquery.corner.js"></script>
<script type="text/javascript" src="/js/jquery-impromptu.1.5.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript">
   jQuery(function($)   
{      
    //擷取select文本和值    
$("#submitBut").click(function(){   
      //注意空格   
    //var strText = $("select[@name=fselect] option[@selected]").text();   
     // var strValue = $("select[@name=fselect] option[@selected]").val();   
      //alert(strText + ":" + strValue);   
        
     /*                          
     //選中值為t3項  
   $("#fselect").attr("value", "t3");  
     //選中第二項  
   $('#fselect')[0].selectedIndex = 1;  
     */  
     alert($("#fselect")[0].length);   
   });   
      
      //select改變時擷取當前選項的值和文本   
   $("#fselect").change(function(){   
      //擷取總的選項   
   //alert($(this)[0].length);   
        
      //擷取的所有的文本   
   var strText = $(this).text();   
      //擷取當前選中值   
   var strValue = $(this).val();   
      //alert(strText + ":" + strValue);   
        
      //選中值為t3項   
   //選中第二項   
   //$(this)[0].selectedIndex = 3;   
      //$(this).attr("value", "t3");   
      / /$("#fselect")[0].options[2].selected = true;   
        
      //獲得當前選中的文本   
   //顯示索引為2的文本   
   var nCurrent = $(this)[0].selectedIndex;   
      alert($("#fselect")[0].options[nCurrent].text);   
      alert(strValue);   
   });   
      
   //增加select   
   $("#add").click(function(){   
     var nLength = $("#fselect")[0].length;   
     var option = document.createElement("option");;   
     option.text = "Text" + (nLength+1).toString();   
     option.value = "t" + (nLength+1).toString();   
     $("#fselect")[0].options.add(option);   
     //$("#fselect").addOption("Text" + (nLength+1).toString(), "t" + (nLength+1).toString(), true);   
   });   
      
   //清空select   
   $("#clear").click(function(){   
     $("#fselect").empty();   
   });   
      
   //清空一項   
$("#remove").click(function(){   
     var index = $("#fselect")[0].selectedIndex;   
     //$("#fselect")[0].remove(index);   
     $("#fselect")[0].options[index] = null;   
   });   
})   
  
</script>

</head>

<body>   
<select name="fselect" id="fselect">   
    <option value='t1'>Test1</option>   
    <option value='t2'>Test2</option>   
    <option value='t3'>Test3</option>   
    <option value='t4'>Test4</option>   
</select>   
<input type="button" name="submitBut" id="submitBut" value="submit" >   
<input type="button" name="add" id="add" value="add" >   
<input type="button" name="clear" id="clear" value="clear" >   
<input type="button" name="remove" id="remove" value="remove" >   
</body>   

</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.