关于下拉框的一些简单使用,希望对您的学习有所帮助
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
- <title>select学习-之传统JS</title>
- <script type="text/javascript" src="jquery1.5.js"></script>
- <script type="text/javascript">
- /**根据游戏显示游戏对应的相应的区!!*/
- function gameselect() {
- //索引,减一是为了忽略第一个“请选择游戏”
- //var index = document.getElementById("gameselect").selectedIndex - 1;
- /*****------把上面一句代码换成Jquery为:-------****/
- var index = $("#gameselect").get(0).selectedIndex - 1;
- //清除所选过的
- // document.getElementById("gameareaselect").options.length = 1;
- /**------把上面一句代码换成Jquery为:---*/
- $("#gameareaselect").get(0).options.length = 1;
- //声明一个数组做测试数据
- var gameareaArray = new Array();
- gameareaArray[0] = ["坦克区1","坦克区2"];
- gameareaArray[1] = ["激战区1","激战区2","激战区3","激战区4"];
- gameareaArray[2] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"];
- for(var i in gameareaArray[index]) {
- var option = new Option(gameareaArray[index][i],gameareaArray[index][i]);
- document.getElementById("gameareaselect").options.add(option);
- }
- }
- function gameareaSelect() {
- var value = document.getElementById("gameareaselect").value;
- alert("下拉框的value值为:" + value );
- //下面我们去拿显示的值
- // var gamearea = document.getElementById("gameareaselect");
- // var showValue = gamearea.options[gamearea.selectedIndex].text;
- /**---把上面两行代码换成下面Jquery形式--*/
- var index = $("#gameareaselect").get(0).selectedIndex ;
- var showValue = $("#gameareaselect").get(0).options[index].text;
- alert("显示的值为:" + showValue);
- }
- </script>
- </head>
- <body>
- <select name="" id="gameselect" οnchange="gameselect()">
- <option value="00">--请选择游戏--</option>
- <option value="10">坦克世界</option>
- <option value="20">激战2</option>
- <option value="30">功夫英雄</option>
- </select>
- <select name="" id="gameareaselect" οnchange="gameareaSelect()">
- <option value="00">--请选择区--</option>
- </select>
- </body>
- </html>
2.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
- <title>select学习-之传统JS</title>
- <script type="text/javascript" src="jquery1.5.js"></script>
- <script type="text/javascript">
- /**根据游戏显示游戏对应的相应的区!!*/
- function gameselect() {
- //var index = document.getElementById("gameselect").value;
- /******-把上面的代码改成Jquery--******/
- var index = $("#gameselect").get(0).value;
- //清除所选过的
- // document.getElementById("gameareaselect").options.length = 1;
- /*****------把上面的代码换成Jquery代码------*****/
- $("#gameareaselect").get(0).options.length = 1;
- //声明一个数组做测试数据
- var gameareaArray = new Array();
- // gameareaArray[0] = ["坦克区1","坦克区2"];
- // gameareaArray[1] = ["激战区1","激战区2","激战区3","激战区4"];
- // gameareaArray[2] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"];
- //数组的下标可以为字符串,比如换成下面的
- gameareaArray['tanke'] = ["坦克区1","坦克区2"];
- gameareaArray['jizhan'] = ["激战区1","激战区2","激战区3","激战区4"];
- gameareaArray['gongfu'] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"];
- for(var i in gameareaArray[index]) {
- var option = new Option(gameareaArray[index][i],gameareaArray[index][i]);
- // var option = new Option(gameareaArray[index][i],"00000");
- document.getElementById("gameareaselect").options.add(option);
- }
- }
- function gameareaSelect() {
- var value = document.getElementById("gameareaselect").value;
- alert("下拉框的value值为:" + value );
- //下面我们去拿显示的值
- // var gamearea = document.getElementById("gameareaselect");
- // var showValue = gamearea.options[gamearea.selectedIndex].text;
- /**---把上面两行代码换成下面Jquery形式--*/
- var index = $("#gameareaselect").get(0).selectedIndex ;
- var showValue = $("#gameareaselect").get(0).options[index].text;
- alert("显示的值为:" + showValue);
- }
- /**删除游戏“功夫英雄”*/
- function delGongfu() {
- $("#gameselect").get(0).remove(3);
- }
- </script>
- </head>
- <body>
- <select name="" id="gameselect" onchange="gameselect()">
- <option value="00">--请选择游戏--</option>
- <option value="tanke">坦克世界</option>
- <option value="jizhan">激战2</option>
- <option id="gongfu" value="gongfu">功夫英雄</option>
- </select>
- <select name="" id="gameareaselect" onchange="gameareaSelect()">
- <option value="00">--请选择区--</option>
- </select>
- <input type="button" value="删除功夫英雄" onclick="delGongfu()" />
- </body>
- </html>
3.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=GBK" />
- <title>jquery之select学习---有自己封装的对select的操作</title>
- <script type="text/javascript" src="jquery1.5.js"></script>
- <script type="text/javascript" src="jquery_select.js"></script>
- <script type="text/javascript">
- /* $(document).ready(function(){
- alert(1);
- });
- jQuery(document).ready(function(){
- alert(2);
- });
- */
- //自己创建jQuery简写
- var $j = jQuery.noConflict();
- $j(document).ready(function(){
- //1.添加options
- $j("#bt").click(function(){
- $j("#demo").addOption("请选择","00");
- $j("#demo").addOption("China","china");
- $j("#demo").addOption("USA","usa");
- //换成Javascript代码如下
- document.getElementById("demo").options.add(new Option("Franch","france"));
- });
- //2.改变options的个数
- $j("#bt1").click(function(){
- //$j("#demo").clearAll();
- //上面一行代码换成JavaScript
- //document.getElementById("demo").options.length = 1;
- //$j("#demo").options.length = 0; 记得这么写是不对的。。。。。应该是下面的写法
- //$j("#demo").get(0).options.length = 1;
- $j("#demo").get(0).remove(3);//删除索引为3的option
- });
- //3.确定options的个数
- $j("#bt2").click(function(){
- // var size = $j("#demo").optionssize();
- //换成javascript为:
- var size = document.getElementById("demo").options.length;
- alert(size);
- });
- });
- //4.确定当前是第几个options
- function getCount() {
- // var index = document.getElementById("demo").selectedIndex;
- //换成对应的jQuery代码为:
- var index = $j("#demo").get(0).selectedIndex;
- var text = $j("#demo").get(0).options[index].text;
- alert(index + ":" + text);
- }
- /*
- jQuery(document).ready(function($){
- $("#bt").click(function(){
- alert(1);
- });
- });
- */
- </script>
- </head>
- <body>
- <input type="button" value="触发" id="bt"/>
- <input type="button" value="消失" id="bt1"/>
- <input type="button" value="options的个数" id="bt2"/>
- <select name="demo" id="demo" onchange="getCount()">
- </select>
- </body>
- </html>
4.
- //get select size
- jQuery.fn.optionssize = function(){
- return jQuery(this).get(0).options.length;
- }
- //get selected index
- jQuery.fn.getSelectedIndex = function(){
- return jQuery(this).get(0).selectedIndex;
- }
- jQuery.fn.getSelectedOption = function(){
- var index = this.getSelectedIndex();
- return jQuery(this).get(0).options[index];
- }
- //get selected text
- jQuery.fn.getSelectedText = function(){
- if(this.optionssize() == 0) return "";
- else{
- var index = this.getSelectedIndex();
- return jQuery(this).get(0).options[index].text;
- }
- }
- //get selected attr
- jQuery.fn.getSelectedAttr = function(attrName){
- if(this.optionssize() == 0) return "";
- else{
- var index = this.getSelectedIndex();
- return jQuery(jQuery(this).get(0).options[index]).attr(attrName);
- }
- }
- //get selected value
- jQuery.fn.getSelectedValue = function(){
- if(this.optionssize() == 0)
- return "";
- else
- return jQuery(this).val();
- }
- //set option value select
- jQuery.fn.setSelectedValue = function(value){
- jQuery(this).get(0).value = value;
- }
- //set option text first select
- jQuery.fn.setSelectedText = function(text)
- {
- var isExist = false;
- var count = this.optionssize();
- for(var i=0;i<count;i++)
- {
- if(jQuery(this).get(0).options[i].text == text)
- {
- jQuery(this).get(0).options[i].selected = true;
- isExist = true;
- break;
- }
- }
- }
- //set selected index
- jQuery.fn.setSelectedIndex = function(index)
- {
- var count = this.optionssize();
- if(index >= count || index < 0)
- {
- jQuery(this).get(0).selectedIndex = -1;
- }
- else
- {
- jQuery(this).get(0).selectedIndex = index;
- }
- }
- //check select has value
- jQuery.fn.isExistItem = function(value)
- {
- var isExist = false;
- var count = this.optionssize();
- for(var i=0;i<count;i++)
- {
- if(jQuery(this).get(0).options[i].value == value)
- {
- isExist = true;
- break;
- }
- }
- return isExist;
- }
- //add select value,text if(this has alert)
- jQuery.fn.addOption = function(text,value)
- {
- if(this.isExistItem(value))
- {
- }
- else
- {
- jQuery(this).get(0).options.add(new Option(text,value));
- }
- }
- //delete select by value if(this has not alert)
- jQuery.fn.removeItem = function(value)
- {
- if(this.isExistItem(value))
- {
- var count = this.optionssize();
- for(var i=0;i<count;i++)
- {
- if(jQuery(this).get(0).options[i].value == value)
- {
- jQuery(this).get(0).remove(i);
- break;
- }
- }
- }
- else
- {
- }
- }
- //delete select by index
- jQuery.fn.removeIndex = function(index)
- {
- var count = this.optionssize();
- if(index >= count || index < 0)
- {
- }
- else
- {
- jQuery(this).get(0).remove(index);
- }
- }
- //delete select by seleced
- jQuery.fn.removeSelected = function()
- {
- var index = this.getSelectedIndex();
- this.removeIndex(index);
- }
- //clear all options
- jQuery.fn.clearAll = function()
- {
- jQuery(this).get(0).options.length = 0;
- }
具体例子请去下载:select总结
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1151983
,如需转载请自行联系原作者