1. 程式人生 > >常用Js筆記,以後可能用得上

常用Js筆記,以後可能用得上

checkbox arc label item 下單 trigger all IT push

  <div class="order_head">
                <table>
                    <thead>
                        <tr>
                            <th class="talb1">
                                商品
                                <label><input type="checkbox" class="tgbox3"
>全選</label> </th> <th>價格</th> <th>數量</th> <th>總額</th> <th>狀態</th> <th>
操作</th> </tr> </thead> </table> </div> <table class="order_body"> <thead> <tr> <
th colspan="6"> <div class="mergeckb"> <input value="255" type="checkbox" class="tgbox" name="mk" /> </div> <p>訂單號:<i>DS180404111619136495</i></p> <p>下單時間:<i>2018/4/4 11:16:19</i></p> </th> </tr> </thead> <tbody> </tbody> </table>
//關鍵字搜索
$("#kwdKeyInput").focus(function () { document.onkeydown = function (evt) { var Withevt = (evt) ? evt : ((window.event) ? window.event : ""); var keyCode = Withevt.keyCode || Withevt.which || Withevt.charCode; if (keyCode == 13) { $(‘.or-search ul li button‘).trigger("click"); } } })
  //全選
    $(".tgbox3").click(function () {
        //取消全選
        if (!this.checked) {
            $(‘input[class="tgbox"]:checkbox:checked‘);
            var cboxs = $(‘input[class="tgbox"]:checkbox:checked‘);
            $.each(cboxs, function (index, e) {
                removeToCookie($(e).val())
            });
        }

        $(‘.tgbox‘).prop("checked", this.checked);
        $(".many").text($("thead input[name=‘mk‘]:checked").length);

        var cboxs = $(‘input[class="tgbox"]:checkbox:checked‘);
        var cids = [];
        $.each(cboxs, function (index, e) {
            cids.push($(e).val());
        });
        addIdToCookie("," + cids);
    });
//選框選中事件
$(‘input[class="tgbox"]‘).click(function () {
        $(".tgbox3").prop("checked", $(‘input[name="mk"]‘).length == $(‘input[name="mk"]:checked‘).length);

        if (this.checked) {
            addIdToCookie("," + $(this).val());
        } else {
            removeToCookie($(this).val());
        }
    });
//數組去重 
function distinct(arr) {
        var len = arr.length;
        arr.sort();
        for (var i = len - 1; i > 0; i--) {
            if (arr[i] == "") { arr.splice(i, 1); }
            if (arr[i] == arr[i - 1]) {
                arr.splice(i, 1);
            }
        }
        return arr;
    }
 //移除數組
    function removeToCookie(id) {
        var cookie = getCookie("orderIds") || "";
        var cookieArray = cookie.split(",");
        var idArray = id.split(",");
        if (cookie.indexOf(id) >= 0) {
            $.each(cookieArray, function (index, item) {
                $.each(idArray, function (index2, iditem) {
                    if (iditem == item) {
                        cookieArray.splice(index, 1);
                    }
                });
            });

            delCookie("orderIds");
            setCookie("orderIds", cookieArray.join(","), 10);
        }
$(function () {
        //加載時選中之前選中項
        $(‘.tgbox‘).each(function () {
            var allCookie = getCookie("orderIds") || "";
            if (allCookie.indexOf($(this).val()) >= 0) {
                $(this).prop(‘checked‘, true);
            }
        });
        //如果選中項等於當前表單所有項, 則說明是全選。
        $(".tgbox3").prop("checked", $(‘input[name="mk"]‘).length == $(‘input[name="mk"]:checked‘).length);
    })

獲取表單某個name為mk的input框

$(‘input[name="mk"]‘)

獲取某個下拉框選中的文本/值

$("#RemitCurrency option:selected").text()

$("#RemitCurrency option:selected").val()

獲取單選框選中的值

$(‘input:radio:checked‘).val()

常用Js筆記,以後可能用得上