例如:
var a = " ";
var b = "";
b = a * 8;
alert(b +"this far to the right");
注意:我不想使用  ,因为 ActiveX FSO 将用于写入文本文件而不是 html 文件。所以它只需要是空格:
我想要实现的目标的更彻底的细节:
我试图让 ActiveX FSO 在表单提交后从 HTML 订单表单写入文本文件,然后继续将订单写入文本文件。文本文件需要采用特定格式,Microsoft Dynamics 才能接受进口销售。
如下所示:客户代码 空格 商品代码 空格 数量 空格:
import.txt 减去带槽的字符串长度 = 要填充的剩余空格。
C242299A *4 white spaces* 2890 *12 white spaces* 20 *6 white spaces*
[------------][----------------][--------]
12 char slots 16 char slots 8 char slots
write.js 将创建此 import.txt 文件(这是我需要帮助的部分)
var customercode = document.getElementById("customercode").value;
var itemcode = document.getElementById("itemcode").value;
var quantity = document.getElementById("quantity").value;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile(path+"import.txt",8,true,0);
//customer code string length must be measured to determine remaining spaces
//to fill before item code can be entered.
//you only have 12 character slots before the next string "item code" can be entered
var remainingSpaces = "";
remainingSpaces = 12 - customercode.length;
spacefill1 = " " * remainingspaces;
remainingSpaces = 16 - itemcode.length;
spacefill2 = " " * remainingSpaces;
remainingSpaces = 8 - quantity.length;
spacefill3 = " " * remainingSpaces;
s.WriteLine(customercode+spacefill1+itemcode+spacefill2+quantity+spacefill3);
应该创建一个如下所示的文本文件:
C242299A 2890 20
然后将其导入到 Microsoft Dynamics。
但问题是它不会将空格相乘,而是将空格视为 0/null :( 欢迎使用 Jquery 解决方案。
最佳答案
要多次重复某个字符,请使用:
var max = 8;//times to repeat
var chr = "a";//char to repeat
console.log(new Array(max + 1).join(chr));//aaaaaaaa
请注意,如果您对空格执行此操作,它们大多会压缩为一个(但它们确实存在)。
您可以使用<pre>
标记以显示每个空格 ( demo )
关于javascript - 如何在 javascript 中乘以空格以便在 ActiveX FSO Write() 方法中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335493/