增加序列号并计算 - javascript

时间:2015-11-18 17:21:48

标签: javascript

我们公司的客户需要特定的条形码校验和。我想出了以下内容,允许用户输入经销商部件号,部件号和序列号。现在,当您单击“保存”时,它将使用校验和正确计算条形码。现在,我们已经添加了一个数量框,以便我们可以打印x个条形码,序列号递增1.(即第一个序列号是000001,接下来是000002,依此类推)。我上周试图做的是更改此代码,以便用户可以输入部件号和sn,它将计算每个条形码到数量。 这就是我们想要的: If user enters part numbers, first serial number and quantity then clicks "Save" this should be the result. At present, this can only be achieved by entering each serial number and clicking save

*注意:虽然可以输入数量,但我的代码中尚未使用实际值...

因此,要实现这一点,我需要找到一种方法来增加序列号而不会丢弃前导零但保持qty.length的长度。 我还需要弄清楚如何使用部件号循环每个新的序列号,以获得条形码的正确校验和。经过一周的盯着这个,我希望一些新鲜和有经验的眼睛可以提供帮助。这是我的代码。

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Capture Form Fields to CSV</title>
<script type="text/javascript">
<!--

function saveValues() {
    var frm = document.form1;
	 var str = frm.text1.value + frm.text2.value + frm.text3.value;
	 var dpn = frm.text1.value;
	 var wpn = frm.text2.value;
	 var wsn = frm.text3.value;
	 var strArray = str.split("");	
	 var calcArray = strArray;
	 var total =0;
	 
	 
for (var i = 0; i < str.length; i++)
strArray[i] = strArray[i].charCodeAt(0);	
	 
for (var i = 0; i < strArray.length; i++){
    if (strArray[i] >= 65 && strArray[i] <= 90){
    calcArray[i] = (strArray[i] - 64) * (i+1)
    }
    else if (strArray[i] >=97 && strArray[i] <=122) {
    calcArray[i] = (strArray[i] - 96) * (i+1)	
    }
    else if (strArray[i] >=48 && strArray[i] <=57) {
    calcArray[i] = (strArray[i] - 48) * (i+1)
    }
    else {
    calcArray[i] = 1 * (i+1)	
    }
    }

for (var i in calcArray){
total += calcArray[i];}  
var mod2 = str.length - (2*(Math.floor(str.length/2))); 
var mod10 = (total + mod2) - (10*(Math.floor((total + mod2)/10))) ;
var chk = mod10;
var record = ""
           + dpn + "," + wpn + "," + wsn + "," +dpn + "~" + wpn + "~" + wsn + "~" + chk + "\n";
            
    frm.textarea1.value += record;
    
}

function clearText() {
    document.form1.textarea1.value = "";P
    form1.text1.value = "";
    form1.text2.value = "";
    form1.text3.value = "";
}

function csvSave() {
    var a = document.createElement('a');
        with (a) {
              href='data:text/csv;base64,' + btoa(document.getElementById('textarea1').value);
              download='csvfile.csv';
          }
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          }

//-->
</script>
</head>
<body>
<h1>Capture Form Fields to CSV</h1>

<form name="form1" >
  <p>
    Distributor Part Number: <input name="text1" type="text" value="GDM1301" /><br />
    Our Part Number: <input name="text2" type="text" value="PCBDM160"/><br />
    Our Serial Number: <input name="text3" type="text" value="000001"/><br />
    Label Quantity: <input name="qty" type="text" value="3"/>    
    
  </p>
  <p>
    <input name="save" type="button" value="Save"
           onclick="saveValues(); return false"/>
    &#0160;
    <input name="clear" type="button" value="Clear"
           onclick="clearText(); return false"/>
    <button onclick="csvSave()">CSV</button>       

  </p>
  <p>
    <i>Click 'Save' to add content</i><br />
    <textarea id="textarea1" cols="80" rows="20"></textarea>         
  </p>
</form>

</body>
</html>

0 个答案:

没有答案