javascript(select对象购物车)

news/2024/7/10 23:58:01 标签: javascript, linq, jquery

JavaScript之Select和Option对象

select对象|option对象

length 长度

selectedIndex 获取选中项的索引(下标)

options 返回一个数组(所有的option项)

options[3] 获取指定下标的option对象

obj.options[index].value; 获取选中项options的value

obj.options[index].text; 获取选中项options的text

obj.add(new Option("文本" ,"值" ));

利用Option对象来创建option标签

javascript">var option = new Option("初中","初中");

javascript"><select id="degree">
            <option value="0">--请选择学历--</option>
            <option value="gaozhong">高中</option>
            <option value="dazhuan">大专</option>
            <option value="benke">本科</option>
            <option value="yanjiusheng">研究生</option>
            <option value="boshi">博士</option>
        </select>
        <input type="button" value="获取下拉列表信息" id="btnGet">
        <input type="button" value="添加选项" id="btnAdd">
        <input type="button" value="清空下拉列表" id="btnClear">
        <input type="button" value="选中本科" id="btnSelect">
        <!-- js区域 -->
        <script type="text/javascript">
            function $(id) {
                return document.getElementById(id);
            }
            // 获取下拉列表信息
            $("btnGet").onclick = function() {
                // alert(123);
                // 获取select标签下的option标签的个数
                     console.log($("degree").length);
​
                // 获取被选中的option的索引(下标)
                     console.log($("degree").selectedIndex);
​
                // 设置选中的option
                     $("degree").selectedIndex = 3;
​
                // 获取option数组
                     console.log($("degree").options);
                // 获取指定元素对象
                     console.log($("degree").options[2]);
​
                     $("degree").selectedIndex = 3;
                 console.log($("degree").options[$("degree").selectedIndex]);
​
                // 获取value值
                     console.log($("degree").options[2].value);
                // 获取文本内容
                     console.log($("degree").options[2].innerHTML);
                     console.log($("degree").options[2].text);
                // 创建一个option标签  createElement+appchild
                var option=new Option('1','2');
                console.log(typeof(option));
            }
                $('btnSelect').onclick=function(){
                    $('degrss').selectedIndex=i;
                    break;
                }
        }
    }

javascript"><h3>3.二级联动[版本2]</h3>
        省:<select id="province" name="province">
            <option selected="selected" value="all">全部</option>
        </select>
        市:
        <select id="city" name="city">
            <option selected="selected" value="all">全部</option>
        </select>
        
        
        <script type="text/javascript">
            // console.log(typeof(citydata));
            for(var pro in citydata){
                $("province").add(new Option(pro,pro))
            }   
            
            var stu = {"name":"张三","sex":"男"};
            
            
            // 设置$("province") 改变事件
            $("province").onchange = function(){
                // 清空第二个下拉框中的所有选项
                $("city").length = 0;
                $("city").add(new Option("请选择","请选择"));
                // 获取所有的省份(遍历)  获取选中的省份
                for(var pro in citydata){
                    if(pro === this.value){
                        // console.log(pro);
                        // console.log(typeof(citydata[pro]));
                        console.log(citydata[pro]);
                        // break;
                        // 遍历得到点击省份的所有城市
                        for(var ct in citydata[pro]){
                            // console.log(citydata[pro][ct]);
                            $("city").add(new Option(citydata[pro][ct],citydata[pro][ct]));
                        }
                    }
                }
            };
        </script>


http://www.niftyadmin.cn/n/1830661.html

相关文章

jQuery(入门选择器)

jQuery&#xff08;入门&选择器&#xff09; 3W1H JavaScript库&#xff1a;封装了很多JS代码&#xff08;类库&#xff09; jQuery&#xff1a;同样是一个轻量级的库&#xff0c;拥有强大的选择器等更多优点&#xff0c;吸引了更多开发者去学习使用它。 官方地址&#…

jQuery($工具方法CSS属性及方法)

jQuery中属性和CSS操作 图片详解&#xff1a; <!-- html代码块 --> <html><head><meta charset"utf-8"><title></title> //设置css样式<style>.demo1 {border: 1px solid red;} ​.demo2 {background-color: yellow;} ​…

【转】 CoreGraphics QuartzCore CGContextTranslateCTM 用法

原文&#xff1a; http://blog.csdn.net/sqc3375177/article/details/25708447CoreGraphics.h一些常用旋转常量#define M_E 2.71828182845904523536028747135266250 e#define M_LOG2E 1.44269504088896340735992468100189214 log 2e#define M_LOG10E 0.43429448190325182765112…

所有Windows7下游戏的全屏问题

Win键R键&#xff0c;打开运行窗口&#xff0c;输入regedit 回车&#xff0c;这样就打开了注册表编辑器&#xff0c;然后&#xff0c;定位到以下位置&#xff1a;HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\GraphicsDrivers\Configuration\在Configuration这上面右键&am…

jQuery(筛选文档处理)

一、jQuery筛选 语法罗列&#xff1a; 1:过滤 ​1.first():获取匹配的第一个元 2.last():获取匹配的最后一个元素 3.eq(N):获取匹配的第N个或者-N个元素 4.filter(selector):筛选出预指定表达式匹配的元素集合 5.has(selector):筛选出包含特定特点的元素的集合 6.not(selecto…

横竖屏切换时候Activity的生命周期的总结

曾经遇到过一个面试题&#xff0c;让你写出横屏切换竖屏Activity的生命周期。现在给大家分析一下他切换时具体的生命周期是怎么样的&#xff1a;1、新建一个Activity&#xff0c;并把各个生命周期打印出来2、运行Activity&#xff0c;得到如下信息onCreate-->onStart-->o…

动态原型

原型中的方法是共享的&#xff0c;如果每次创建构造函数时不必在重新在原型中添加方法 function Person(name,age){this.name name;this.age age;//方法if(typeof this.sayName ! "function"){Person.prototype.sayName function(){alert();};}} 这样在new Person…

jQuery(事件动画)

一、事件 1.加载Dom两种方式 &#xff08;1&#xff09;window.onload方式 &#xff08;2&#xff09;jQuery方式 // jQuery的加载函数 2种 一个页面可以出现多次 // 加载函数$(function() {// 1.标签绑定绑定事件的方式// (1)直接调用click点击事件的方法即可$("#bt…