JQuery 动态生成表格 带 AJAX

时间:2015/12/3 22:44:00来源:互联网 作者:flyso 点击: 1946 次
//2012-8-3/4 by flyso
/// <reference path="../../../../Toolkit/JQuery/jquery-1.8.1-vsdoc.js" />
//QueryString
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
function UrlDecode(str) {
    var ret = "";
    for (var i = 0; i < str.length; i++) {
        var chr = str.charAt(i);
        if (chr == "+") {
            ret += " ";
        }
        else if (chr == "%") {
            var asc = str.substring(i + 1, i + 3);
            if (parseInt("0x" + asc) > 0x7f) {
                ret += asc2str(parseInt("0x" + asc + str.substring(i + 4, i + 6)));
                i += 5;
            }
            else {
                ret += asc2str(parseInt("0x" + asc));
                i += 2;
            }
        }
        else {
            ret += chr;
        }
    }
    return ret;
}
$("document").ready(function() {
    //第六个表格的删除按钮事件绑定
    $("#flyso_tbody .del").click(function() {
        $(this).parents(".repeat").remove();
    });
    //var buyers = decodeURIComponent(GetQueryString("buyer"));
    //$("#countnums").append("购买者:" + buyers);
    //第六个表格的添加按钮事件绑定
    $("#txtBarCode").keydown(function(e) {

        if (e.keyCode == 13) {

            var BarCode = $("#txtBarCode").val()

            if (BarCode == null || BarCode == "") {
                ScanTxt("请扫描票的信息");
                return;
            }
            var q = $("#flyso_tbody").filter(":contains('" + BarCode + "')").length;
            if (q > 0) {
                ScanTxt("该票已经扫描!");
                return;
            }
            //Load ajax.....
            InitScanTxt("正在扫描信息...");
            $.ajax({
                type: "POST",
                url: "jsonaspx/GetInfoByBarCode.aspx",
                data: { BarCode: BarCode },
                success: function(data) {
                    data = eval("(" + data + ")");
                    var o = data.msg;
                    if (data.ret == 0) {
                        //window.location = document.URL;
                        //alert("找不到匹配信息!");
                        ScanTxt("找不到匹配信息");
                        //setTimeout(function() { $("#txtBarCode").attr("value", ""); }, 90);
                        //$("#txtBarCode").attr("value", "");
                        //window.location.reload();
                    }
                    else if (data.ret == 2) {
                        //window.location = document.URL;
                        //alert("找不到匹配信息!");
                        ScanTxt("该票已经销售!");
                        //setTimeout(function() { $("#txtBarCode").attr("value", ""); }, 90);
                        //$("#txtBarCode").attr("value", "");
                        //window.location.reload();
                    }
                    else if (data.ret == 1) {
                        for (var i = 0; i < o.length; i++) {

                            var vid = o[i].vid; v1 = o[i].v1; v2 = o[i].v2; v3 = o[i].v3; v4 = o[i].v4; v5 = o[i].v5; v6 = o[i].v6;

                            //create table start
                            $("#flyso_tbody>.template")
                            //连同事件一起复制
                .clone(true)
                            //去除模板标记
                .removeClass("template")
                            //修改内部元素
                .find(".row1")
                    .html("<input type=\"hidden\" name=\"sid\" value=\"" + vid + "\" />" + v1 + "<span style=\"display:none;\">" + v6 + "</span>")
                    .end()
                .find(".row2")
                    .text(v2)
                    .end()
                .find(".row3")
                    .text(v3)
                    .end()
                .find(".row4")
                    .text(v4)
                    .end()
                .find(".row5")
                    .text(v5)
                    .end()
                .find(".del")
                    .text("删除")
                    .end()
                            //插入表格
                .appendTo($("#flyso_tbody"))
                            //create table end
                            ScanTxtSuccess("扫描成功!");
                            //setTimeout(function() { $("#txtBarCode").attr("value", ""); }, 90);

                        }
                    }

                    else {
                        alert(data.msg);
                    }
                }
            });
        }
    });
}
)

function InitScanTxt(txt) {
    //$("#txtBarCode").attr("value", txt);
    $("#hinttxt").html(txt);
}

function ScanTxtSuccess(txt) {
    $("#hinttxt").html(txt);
    $("#txtBarCode").attr("value", "");
    $("#txtBarCode").focus()
}
function ScanTxt(txt) {
    $("#hinttxt").html(txt);
    $("#txtBarCode").attr("value", "");

}        


Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3