:
[2009-08-15] 欢迎光临 『踏出校门之后...』----一起表达你和我对网络的热爱,一起追寻你和我痴狂的梦想,炎炎夏日让我们一起感受存在的美丽,我们的极限就是达到唯美... [2009-07-31]   温馨笑容:   热烈欢迎从远到而来的朋友,一起分享你我的喜悦,共享你我的资源。

Javascript生成产品条形码

CSS 部分:
程序代码 程序代码
<style>
body { font-family:"微软雅黑"; font-size:15px; color:#333333}
</style>

JS 部分:
程序代码 程序代码

<script>
function mycode(strTmp,height,width)
    {
    if(document.getElementById("OK").value < 10)
        strTmp = "000"+document.getElementById("OK").value+strTmp;
        else
            if(document.getElementById("OK").value < 100)
                strTmp = "00"+document.getElementById("OK").value+strTmp;
                else
                    if(document.getElementById("OK").value < 1000)
                        strTmp = "0"+document.getElementById("OK").value+strTmp;
                        else
                            strTmp = document.getElementById("OK").value+strTmp;
    var code = strTmp;
    strTmp = "*"+strTmp+"*";
    strTmp = strTmp.toLowerCase();
    strTmp = strTmp.replace(/0/g, "_|_|__||_||_|");
    strTmp = strTmp.replace(/1/g, "_||_|__|_|_||");
    strTmp = strTmp.replace(/2/g, "_|_||__|_|_||");
    strTmp = strTmp.replace(/3/g, "_||_||__|_|_|");
    strTmp = strTmp.replace(/4/g, "_|_|__||_|_||")
    strTmp = strTmp.replace(/5/g, "_||_|__||_|_|");
    strTmp = strTmp.replace(/7/g, "_|_|__|_||_||");
    strTmp = strTmp.replace(/6/g, "_|_||__||_|_|");
    strTmp = strTmp.replace(/8/g, "_||_|__|_||_|");
    strTmp = strTmp.replace(/9/g, "_|_||__|_||_|");
    strTmp = strTmp.replace(/a/g, "_||_|_|__|_||");
    strTmp = strTmp.replace(/b/g, "_|_||_|__|_||");
    strTmp = strTmp.replace(/c/g, "_||_||_|__|_|");
    strTmp = strTmp.replace(/d/g, "_|_|_||__|_||");
    strTmp = strTmp.replace(/e/g, "_||_|_||__|_|");
    strTmp = strTmp.replace(/f/g, "_|_||_||__|_|");
    strTmp = strTmp.replace(/g/g, "_|_|_|__||_||");
    strTmp = strTmp.replace(/h/g, "_||_|_|__||_|");
    strTmp = strTmp.replace(/i/g, "_|_||_|__||_|");
    strTmp = strTmp.replace(/j/g, "_|_|_||__||_|");
    strTmp = strTmp.replace(/k/g, "_||_|_|_|__||");
    strTmp = strTmp.replace(/l/g, "_|_||_|_|__||");
    strTmp = strTmp.replace(/m/g, "_||_||_|_|__|");
    strTmp = strTmp.replace(/n/g, "_|_|_||_|__||");
    strTmp = strTmp.replace(/o/g, "_||_|_||_|__|");
    strTmp = strTmp.replace(/p/g, "_|_||_||_|__|");
    strTmp = strTmp.replace(/r/g, "_||_|_|_||__|");
    strTmp = strTmp.replace(/q/g, "_|_|_|_||__||");
    strTmp = strTmp.replace(/s/g, "_|_||_|_||__|");
    strTmp = strTmp.replace(/t/g, "_|_|_||_||__|");
    strTmp = strTmp.replace(/u/g, "_||__|_|_|_||");
    strTmp = strTmp.replace(/v/g, "_|__||_|_|_||");
    strTmp = strTmp.replace(/w/g, "_||__||_|_|_|");
    strTmp = strTmp.replace(/x/g, "_|__|_||_|_||");
    strTmp = strTmp.replace(/y/g, "_||__|_||_|_|");
    strTmp = strTmp.replace(/z/g, "_|__||_||_|_|");
    strTmp = strTmp.replace(/-/g, "_|__|_|_||_||");
    strTmp = strTmp.replace(/\*/g, "_|__|_||_||_|");
    strTmp = strTmp.replace(/\//g, "_|__|__|_|__|");
    strTmp = strTmp.replace(/\%/g, "_|_|__|__|__|");
    strTmp = strTmp.replace(/\+/g, "_|__|_|__|__|");
    strTmp = strTmp.replace(/\./g, "_||__|_|_||_|");
    strTmp = strTmp.replace(/\_/g, "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
    strTmp = strTmp.replace(/\|/g, "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>");
    document.all.tm.innerHTML = strTmp+"<br>"+code;
    }
    
function createbarcode()
{    
    var e1 = document.getElementById("OK").value;
    var e2 = document.getElementById("NO").value;
    if(e1==""||e2==""){
        alert("请输入条码编号!");
        return false;
    }
    else{
        mycode(e2,25,1);
        var win = window.open('', '_blank', '');
        win.document.write(tm.innerHTML);//把层的内容(表格)写到新窗口
        win.document.execCommand('saveas','',title+'.htm');
        win.close();//关闭新窗口
    }
}
function check ()
{    
    var e1 = document.getElementById("OK").value;
    var e2 = document.getElementById("NO").value;
    if(e1.length >6){
        alert("只能输入四个字符");
        document.getElementById("OK").value = "";
        return false;
    }
    if(e2.length >16){
        alert("只能输入13个字符");
        document.getElementById("NO").value = "";
        return false;
    }

}

</script>


HTML 部分:
程序代码 程序代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>扫描条码生成</title>
</head>
<body>
<div id="tm"></div>
编码一区:<input id="OK" style="width:60px"  onblur="check();" />
编码二区:<input id="NO" style="width:160px" onblur="check();" /><input type="button" value="生成条码" onclick="createbarcode();" /><br />
说明:<br />
编码一区编码规则为:   CNSXJB                 中国陕西靖边<br />
编码二区编码规则为:   CGCY-20100525001     残戈创意-20100525001(后三位为流水号)    
</body></html>


演示效果:
HTML代码


[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]



[本日志由 confusion 于 2010-07-13 04:47 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: javaScript 条形码 生成条码
评论: 0 | 引用: 0 | 查看次数: 106
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭