欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jquery對(duì)select的增、刪、改、查操作

 更新時(shí)間:2015年02月06日 15:02:38   投稿:hebedich  
這篇文章主要介紹了Jquery對(duì)select的增、刪、改、查操作的方法,需要的朋友可以參考下

逃不開傳統(tǒng)的四種操作:增、刪、改、查。

<四處搜刮了jquery對(duì)select操作的代碼,匯集一下,方便以后查看。日歷天數(shù)變化代碼為原創(chuàng)。>

[增]:

復(fù)制代碼 代碼如下:

$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個(gè)Option(下拉項(xiàng))
$("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個(gè)Option(第一個(gè)位置)

[刪]:

復(fù)制代碼 代碼如下:

$("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個(gè))
$("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個(gè))
$("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
$("#select_id").empty(); //清空

[改](設(shè)置選中項(xiàng)):

復(fù)制代碼 代碼如下:

$("#select_id ").get(0).selectedIndex=1; //設(shè)置Select索引值為1的項(xiàng)選中
$("#select_id ").val(4); //設(shè)置Select的Value值為4的項(xiàng)選中
$("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置Select的Text值為jQuery的項(xiàng)選中

[查](獲取選中值):

1.獲取選中項(xiàng)的value

復(fù)制代碼 代碼如下:

$("#select_id").val(); //獲取選中項(xiàng)的value
$("#select_id ").get(0).selectedIndex; //獲取選中項(xiàng)的索引值
$("#select_id").find("option:selected").text(); //獲取選中項(xiàng)的text
$("#select_id option:last").attr("index"); //獲取Select最大的索引值

附上代碼樣例,代碼功能為根據(jù)實(shí)際選擇的“年”、“月”,來改變select“日”中option的天數(shù)。

使用php編寫,默認(rèn)$("select.day")初始有31天,因?yàn)槟J(rèn)為1月:

復(fù)制代碼 代碼如下:

<select name ="date_year" class="year"> //年
    <?php
    for ($year = 1990; $year <= date("Y"); ++$year) {
    ?>
        <option value="<?php echo $year;?>"><?php echo $year;?></option>
    <?php
    }
    ?>
</select>

復(fù)制代碼 代碼如下:

<select name ="date_month" class="month"> //月
    <?php
    for ($month = 1; $month <= 12; ++$month) {
    ?>
        <option value="<?php echo $month;?>"><?php echo $month;?></option>
    <?php
    }
    ?>
</select>

復(fù)制代碼 代碼如下:

<select name ="date_day" class="day"> //日
    <?php
    for ($day = 1; $day <= 31; ++$day) {
    ?>
        <option value="<?php echo $day;?>"><?php echo $day;?></option>
     <?php
     }
     ?>
</select>

jquery代碼:

復(fù)制代碼 代碼如下:

$(document).ready(function() {
    $("select.month, select.year").change(function() { //"年"、"月"的變化都可能引起“日”的變化
        $("select.day").empty(); //非常重要,要先清空
        for (var i = 1; i < 31; i++) {
            var option = $("<option>").val(i).text(i);
            $("select.day").append(option);
        }
        var month = $("select.month").val();
        if ((month % 2 && month < 8) || (month % 2 === 0 && month > 7)) {
            $("select.day").append("<option value='31'>31</option>"); //天數(shù)為31天的append一個(gè)option
        }
        if (month === 2) {
            $("select.day option:last").remove();
            $("select.day option:last").remove(); //2月天數(shù)28
            var year = $("select.year").val();
            if ((year % 4 === 0 && year % 100) || year % 400 === 0)
                $("select.day").append("<option value='29'>29</option>"); //閏年2月天數(shù)加一
        }
    });
});

以上就是關(guān)于jQuery實(shí)現(xiàn)對(duì)select的增、刪、改、查操作總結(jié),希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論