posts - 93,  comments - 2,  trackbacks - 0
val()方法取值,<select>元素默认的为value属性值,否则为<option value="">value</option>;<input type='checkbox' value="">默认为value属性值,否则为on;<input type="text" value=""></input>默认为输入到文本框里的值;<input type="radio" id="abc" value="">abc</input>默认为value属性值,否则为on;
1.处理Select事件
         <select id="select">
            <option>苹果</option>
            <option>栗子</option>
            <option>桃子</option>
          </select>
            当$('#select').val();默认的值为选择的值,如“苹果”,但<option value="冬瓜">苹果</option>在选择苹果是$('#select').val();得到的是“冬瓜”。
        (1)为select添加选项值,如下:
            <html>
            <head>    
                    <title>添加选项</title>
                    <script type="text/javascript">
                       $(document).ready(function(){
                        $(".info").append($("<option></option>").attr("value","1").text("苹果"));
                        });
                    </script>
            </head>
            <body>
                    <form>
                            <select class="info"></select>
                    </form>
            </body>
            </html>
         (2)获取select中的被选项,如下:
            <html>
            <head>    
                    <title>获取被选项</title>
                    <script type="text/javascript">
                       $(document).ready(function(){
                            $("#bt").click(function(){
                                var fruit=$("select option:selected").val();
                                alert(fruit);
                            });
                    </script>
            </head>
            <body>
                    <form>
                            <select class="info">
                                    <option>苹果</option>
                                    <option>栗子</option>
                                    <option>桃子</option>
                            </select>
                            <input type="button" id="bt">按钮</input>
                    </form>
            </body>
            </html>
2.处理checkbox事件
        (1)全选uncheck/checkAll
         <div class="infobox"><input type="checkbox" name="a" id="a" value=1>看电影</input></div>
         <div class="infobox"><input type="checkbox" name="b" id="b" value=2>打篮球</input></div>
         <div class="infobox"><input type="checkbox" name="c" id="c" value=3>看书</input></div>
         <div class="infobox"><input type="checkbox" id="checkAll">Check/Uncheck</input></div>
         $(document).ready(function(){
           $('#checkAll').click(function(){
                $("input[type='checkbox']").attr('checked',$("#checkAll").is(':checked'));
          });
       });
        (2) 检查复选框是否被选中
        $('form').find(":checkbox").each(function(){
            if($(this).is(":checked");
        });
3.处理radio事件
    (1)检查单选按钮是否被选择
        var a=$("input[type='radio']:checked").val();
        
posted on 2011-07-21 21:02 Terry Zou 阅读(554) 评论(0)  编辑  收藏 所属分类: jQuery

只有注册用户登录后才能发表评论。


网站导航:
 
<2011年7月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜