$(function () {
    var total = 0, height = $(window).height(), memberScroll, cartScroll, proScroll;
    $.cart = {
        succ: function (data, status, xhr) {
            data.Status == 1 ?
   $.dialog(data.Message, { header: { title: "添加商品", closebtn: "×" } }) :
   data.Status == 0 ?
    $.cart.set.apply(
     data.Target.Product ?
     $.cart.add(data.Target.Product) :
     $(".ordBody01 tbody tr[data-ProductNo=" + data.Target.CartProduct.ProductNo + "]"),
     [data.Target.CartProduct]
    ) :
    $.cart.selpro(data.Target.Products);
        },
        add: function (pro) {
            var tr = $("<tr></tr>").attr('data-ProductNo', pro.ProductNo).appendTo(".ordBody01 tbody");
            $('<td width="38%" class="name"></td>').html(pro.Name).appendTo(tr);
            $('<td width="20%" class="price"><input type="number" class="priTxt" readonly="readonly" value="' + pro.SalePrice + '"/></td>').appendTo(tr);
            var td = $('<td width="30%" class="proAmount"></td>').appendTo(tr);
            $('<p><button type="button" class="qtyDown disabled"></button><input type="tel" name="qty" value="1" class="qtyTxt"><button type="button" class="qtyUp"></button></p>').appendTo(td);
            $('<td><button type="button" class="btnDelete"></button></td>').appendTo(tr);
            tr.find(".priTxt").bind("change", $.cart.priceChange);
            tr.find(".qtyTxt").bind("change", $.cart.qtyChange);
            tr.find(".qtyDown,.qtyUp").bind("click", $.cart.qtyChange);
            tr.find(".btnDelete").bind("click", $.cart.cartDelete);
            return tr;
        },
        priceChange: function (e) {
            e.preventDefault();
            var that = $(this),
   val = that.val();
            val = /\d+/.test(val) && val > 0 ? parseFloat(val) : 0;
            that.val(val);
            $.post("/Order/SetPrice", { ProductNo: that.closest("tr").attr("data-ProductNo"), Price: val });
            $.cart.cartCalc();
        },
        qtyChange: function (e) {
            e.preventDefault();
            var that = $(this), inp = that.closest("p").find(".qtyTxt");
            !that.hasClass("disabled") && inp.cartChange(e, that.hasClass("qtyDown") ? -1 : that.hasClass("qtyUp") ? 1 : 0);
            $.cart.cartCalc();
        },
        set: function (cpro) {
            $("#word").val("");
            $(this).find(".qtyTxt").val(cpro.Quantity).change();
        },
        pay: function (data, status, xhr) {
            $.dialog(data, { header: { title: "订单确认", closebtn: '&times;' }, footer: { closebtn: '取消', okbtn: '确定', } }, function () {
                var pay = this.$element.find("input.pay").val();
                $.post("/Order/BuildOrder", { Payment: pay }, function (res) {
                    $.dialog(res.Message, { header: { title: "支付成功", closebtn: '&times;' }, close: res.Status ? null : function () { location.reload() } })
     .$element
     .dialogLoaded()
                });
            });
        },
        dialogLoaded: function () {
            var that = $(this);
            that.find("[data-page=true]").click(function (e) {
                e.preventDefault(), $(this).navigate({
                    callback: function () {
                        var $that = $(this);
                        $that.find(".ordDetListBody").height($(window).height() - 240);
                        $that.find(".btnClose").click(function () { location.reload() })
                    }
                })
            });
        },
        selpro: function (pros) {
            var div = $('<div style="width:100%;height:100%;overflow:scroll"></div>');
            var ul = $('<ul class="productList"></ul>').appendTo(div);
            for (var i in pros) {
                $("<li class='clearfix' data-productno='" + pros[i].ProductNo + "'><span class='floatL'>" + pros[i].Name + "</span><span class='floatR'>" + pros[i].SalePrice + "</span></li>").appendTo(ul).bind("click", $.cart.addpro);
            }
            var d = $.dialog(div, { header: { title: "选择商品", closebtn: "&times;" } });
            if (proScroll) proScroll.destroy();
            proScroll = new iScroll(d.find(".productList")[0], { desktopCompatibility: true });
        },
        addpro: function (e) {
            e = e || event;
            var that = $(e.target).closest("li"), no = that.attr("data-productno");
            $.post('/Order/AddCart', { ProductNo: no }, $.cart.succ);
            var el = $(that).closest(".modal"), wr = el.prev();
            el.remove(), wr.remove();
        },
        cartChange: function (e, i) {
            i = i || 0;
            var that = $(this),
   val = that.val(),
   init = that.attr("data-init") || 0,
   dbtn = that.prev(".qtyDown"),
   ubtn = that.next(".qtyUp");
            oval = that.attr("oval") || val;

val = (/\d+/.test(val) && val > 1 ? parseInt(val) : 1) + i;

that.val(val), dbtn.toggleClass("disabled", val <= 1);
            init && $.post("/Order/SetCart", { ProductNo: that.closest("tr").attr("data-ProductNo"), Amount: val });
            that.attr("data-init", 1)
        },
        cartAdd: function (e) {
            var that = $(e.currentTarget).closest("li"), no = that.attr("data-productno");
            $.post('/Order/AddCart', { ProductNo: no }, $.cart.succ);
            var el = $(that).closest(".modal"), wr = el.prev();
            el.remove(), wr.remove();
        },
        cartClear: function () {
            $.post("/Order/ClsCart")
        },
        cartDelete: function (e) {
            e.preventDefault();
            var that = $(this).closest("tr");
            $.post("/Order/DelCart", { ProductNo: that.attr("data-ProductNo") });
            that.remove();
            $.cart.cartCalc();
        },
        cartCalc: function () {
            var sum = 0;
            $(".ordBody01 tr").each(function () {
                var pri = $(".priTxt", this).val(), qty = $(".qtyTxt", this).val();
                sum += pri * qty;
            });
            $(".ordTotal span").html(sum.toString().replace(/(\d+)(\.)(\d{0,2}).*$/, "$1$2$3"));
        },
        loadmem: function (data) {
            $(".memList").html(data).memberLoaded()
        },
        memberRemove: function (e) {
            e.preventDefault();
            var ord = $('.ordMember'), $btn = $('.btnAddMember')
            ord.remove(), $btn.show();
            $("#word").attr({ "disabled": "disabled", "placeholder": "请先添加会员" });
            $.post("/Order/EmptyMember")
        },
        memberSick: function (e) {
            e.preventDefault();
            var ord = $(this).closest('.ordMember');
            $.post("/Order/MemberSick", { Id: ord.attr('data-MemberId') }, function (d) {
                $.dialog(d, { header: { title: '会员病史', closebtn: '&times;' } })
            })
        },
        memberLoaded: function () {
            var that = $(this);
            that.find("li[data-memberid]").click(function (e) {
                var $that = $(this), ord = $('.ordMember'), memberId = $that.attr("data-MemberId"), memberLevel = $that.attr('data-MemberLevel'), $btn = $('.btnAddMember'), page = that.closest(".page"), linka = $that.find('.link a');
                if (linka.size()) return (location.href = linka.attr('href'));
                if (!ord.size()) ord = $('<div class="ordMember clearfix"></div>').insertAfter($btn);
                ord.attr("data-MemberId", memberId).html(''),
    $('<h2></h2>').text($('.name', $that).text()).appendTo(ord),
    $('<button type="button" class="btnCancelMem"></button>').appendTo(ord).bind("click", $.cart.memberRemove),
    $('<button type="button" class="btnStyle02 floatR btnMedRec">查看病历</button>').appendTo(ord).bind("click", $.cart.memberSick),
    $('<span></span>').text(memberLevel).appendTo(ord),
    $btn.hide(),
    $("#word").attr({ "disabled": null, "placeholder": "请输入商品代码" }),
    $.post("/Order/SetMember", { MemberId: memberId });
                setTimeout(function () { page.remove() });
            });
        },
        goBack: function () {
            $(this).click(function (e) {
                e.preventDefault();
                var that = $(this).closest(".page").removeClass("active");
                setTimeout(function () { that.remove() }, 300);
            })
        },
        navigate: function (options) {
            $(".modal,.modal-backdrop").remove();
            var remote = $(this).attr("data-toggle") || $(this).attr("href");
            $.get(remote, function (data) {
                var p = $('<div class="page"></div>').html(data).appendTo("#dvContent").addClass("active");
                options && options.callback && options.callback.apply(p);
            })
        },
    }
    $.fn.goBack = $.cart.goBack;
    $.fn.navigate = $.cart.navigate;
    $.fn.memberLoaded = $.cart.memberLoaded;
    $.fn.cartChange = $.cart.cartChange;
    $.fn.dialogLoaded = $.cart.dialogLoaded;

$(".ordBody01").height(height - 240);
    $(".ordBody01 .priTxt").bind("change", $.cart.priceChange).change();
    $(".ordBody01 .qtyTxt").bind("change", $.cart.qtyChange).change();
    $(".btnEmpty").click(function (e) {
        e.preventDefault();
        var t = $(".ordBody01 tr");
        !t.size() ?
  $.dialog("订单还不没有商品呢", { header: { title: "清空订单", closebtn: "&times;" } }) :
  $.dialog("清空订单商品?", { footer: { okbtn: "确定", closebtn: "取消" }, header: { title: "清空订单", closebtn: "&times;" } }, function () {
      $.cart.cartClear(), t.remove(), $.cart.cartCalc()
  });
    });
    $(".btnAddMember").click(function (e) {
        e.preventDefault(); $(this).navigate({
            callback: function () {
                var that = $(this);
                that.find(".btnBack").goBack();

that.find(".wraper").height(height - 110);
                that.find(".memPhoneSearch input")
    .keypress(function (e) {
        var chars = "0123456789";
        var chr = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode);
        return e.ctrlKey || e.metaKey || (chr < ' ' || chars.indexOf(chr) > -1);
    })
    .bind('keyup change', function (e) {
        var val = this.value, len = val.length, mem = that.find(".memPhoneSearch");
        mem.toggleClass('cur', len > 0);
        this.maxLength = /^1/i.test(val) ? 11 : 12;
    });
                that.find(".btnMemPhoClear").click(function () {
                    that.find(".memPhoneSearch input").val('').focus();
                    that.find(".memPhoneSearch").removeClass("cur");
                })
                that.find(".btnMemSearch").click(function (e) { e.preventDefault(), $(".memPhoneSearch form").submit() });

if (memberScroll) memberScroll.destroy();
                memberScroll = new iScroll(that.find(".scroll")[0], { checkDOMChanges: true });
            }
        });
    });
    $(".btnSubmitOrder").click(function (e) {
        e.preventDefault();
        var procnt = $(".ordBody01 tr").size(), mem = $(".ordMember").size();
        if (!mem) {
            return $.dialog("您没有选择会员", { header: { title: "提交订单", closebtn: "&times;" } });
        }
        if (!procnt) {
            return $.dialog("您没有添加商品", { header: { title: "提交订单", closebtn: "&times;" } });
        }
        $(".ordTotal form").submit();
    });
    $(".btnCancelMem").bind("click", $.cart.memberRemove);
    $(".btnMedRec").bind("click", $.cart.memberSick);
    $(".btnDelete").bind("click", $.cart.cartDelete);
    $(".qtyDown,.qtyUp").bind("click", $.cart.qtyChange);
    $("#word").keyup(function (e) {
        var val = this.value, len = val.length;
        $(".searchTxt").toggleClass('cur', len > 0);
    });
    $(document).on("submit", "form", function () {
        $(":focus").blur();
    })
    cartScroll = new iScroll($(".ordBody01 table")[0], { checkDOMChanges: true });
})

javascript、jQuery的扩展方法,扩展实例展示代码的更多相关文章

  1. 一个利用扩展方法的实例:AttachDataExtensions

    扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...

  2. jQuery中on()方法用法实例

    这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能.定义及在匹配元素上绑定一个或者多个事件处理函数的使用技巧,需要的朋友可以参考下 本文实例讲述了jQuery中on()方法 ...

  3. jQuery中on()方法用法实例详解

    这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能及各种常见的使用技巧,并对比分析了与bind(),live(),delegate()等方法的区别,需要的朋友可以参考下 本 ...

  4. .NET:不要使用扩展方法扩展Object对象。

    C#的扩展方法算是一种Minin(掺入)机制,掺入方法有其合理的使用场景,这里说说一种不好的使用场景(个人意见):不要使用扩展方法扩展Object对象.扩展Object会对所有类型的示例有侵入,特别是 ...

  5. jQuery对象进行方法扩展

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>01 ...

  6. jQuery全局进行方法扩展

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>01 ...

  7. JavaScript | JQuery插件定义方法

    参考 http://www.2cto.com/kf/201507/417874.html ——————————————————————————————————————————————————————— ...

  8. jQuery中eq()方法用法实例

    本文实例讲述了jQuery中eq()方法用法.分享给大家供大家参考.具体分析如下: 此方法能够获取匹配元素集上的相应位置索引的元素. 匹配元素集上元素的位置索引是从0开始的. 语法结构: 复制代码 代 ...

  9. jQuery中attr()方法用法实例

    本文实例讲述了jQuery中attr()方法用法.分享给大家供大家参考.具体分析如下: 此方法设置或返回匹配元素的属性值. attr()方法根据参数的不同,功能也不同. 语法结构一: 获取第一个匹配元 ...

  10. jQuery中index()方法用法实例

    本文实例讲述了jQuery中index()方法用法.分享给大家供大家参考.具体分析如下: 此方法可以搜索匹配元素,并返回元素的索引值.索引值是从0开始的. 语法结构一: 当此方法没有参数的时候,返回值 ...

随机推荐

  1. 程序员---C语言细节7(增加两个整数溢出检测)

    主要内容:增加两个整数溢出检测 #include <stdio.h> #include <limits.h> int main(int argc, char *argv[]) ...

  2. DHCP Option 60 认识

    原文地址: http://blog.163.com/chenqioulin_1983/blog/static/83216232010109104430251/   首先还是看看RFC咋说的吧.DHCP ...

  3. Python 显示LinkedIn用户作业

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-18 @author: guaguastd @name: j ...

  4. retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=10, sleepTime=1 SECONDS)

    [root@qa bin]# hadoop fs -ls / Warning: $HADOOP_HOME is deprecated. 14/07/29 13:25:35 INFO ipc.Clien ...

  5. crm操作观点

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages; /// <summary> ...

  6. 文章13称号 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 开放Nginx在文件夹列表功能

    nginx在列出的默认同意整个文件夹.你怎么转Nginx在文件夹列表功能?打开nginx.conf文件.在location server 要么 http段增加 autoindex on;另外两个參数最 ...

  8. Android应用UI架构

    这个标题听起来可能有点大.事实上这里主要就是讨论一个应用程序的UI组件,是全用Activity还是全用Fragment.或者是二者皆有.以及使用Activity和Fragment的一些注意事项.  A ...

  9. Debian/Ubuntu 已安装gcc/g++ 4.8.1

    gcc 4.8.1 是第一个全然支持C++11(C++14非常可能在gcc 4.9.0開始支持.)的编译器,Windows上能够安装mingw版的.在sourceforge 上有下载.安装也比較方便. ...

  10. UDP(socket)数据访问和封装情况C++代码

     配置QT下的pro文件 TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt   LIBS += -lWs2_32 ...