如何将函数从 vbscripts 转换为 javascript

How to convert a function from vbscripts to javascript

我有一个 vbscript 函数,我需要将其转换为 javascript 函数。该函数使用 eval() 并且将那部分隐藏为 javascript

我一直很困惑

需要重写这个,因为当前函数在 chrome 浏览器上不工作。

我只需要这部分代码的帮助

     locParts = ""
        locAisle = RTrim(LTrim(eval("document.all.bbaisle" & x & ".value"))) & ""
        locBay = RTrim(LTrim(eval("document.all.bbbay" & x & ".value"))) & ""
        locLevel = RTrim(LTrim(eval("document.all.bblevel" & x & ".value"))) & ""
        locBin = RTrim(LTrim(eval("document.all.bbbin" & x & ".value"))) & ""

Function buildLocation(x)

    test = eval("document.all.bbtype" & x & ".value") & ""

    if test = "A" then

    ' This is a multipart location that needs to be assembled prior to validation back in calling procedure
    locParts = ""
    locAisle = RTrim(LTrim(eval("document.all.bbaisle" & x & ".value"))) & ""
    locBay = RTrim(LTrim(eval("document.all.bbbay" & x & ".value"))) & ""
    locLevel = RTrim(LTrim(eval("document.all.bblevel" & x & ".value"))) & ""
    locBin = RTrim(LTrim(eval("document.all.bbbin" & x & ".value"))) & ""

    if locAisle <> "" then
        locParts = locParts & locAisle & "_"
    end if

    if locBay <> "" then
        locParts = locParts & locBay & "_"
    end if

    if locLevel <> "" then
        locParts = locParts & locLevel 
    end if

    if locBin <> "" then
        locParts = locParts & "_" & locBin
    end if

    if locParts <> "" then
        Execute("document.all.bb" & x & ".value=" & CHR(34) & UCASE(locParts) 
        & CHR(34))
    end if

    buildLocation = 1

else

    ' This is either an existing KT location or Offsite so do nothing
    buildLocation = 1

end if    

End Function

这是我在 javascripts 中想到的,但我怀疑如果我将它复制到其他变量,它是否会起作用。

locParts = ""
locAisle = eval("document.getElementById('bbaisle')"+ x +".value").trim()

这样的东西可能对你有用,但如果没有 HTML 标记就很难测试。

function getField(name) {
  return document.getElementById(name) || document.getElementsByName(name)[0];
}

function getTrimmedFieldValue(name) {
  const field = getField(name);
  return (field ? field.value : "").trim();
}

function buildLocation(x) {
  const type = getTrimmedFieldValue("bbtype" + x);
  if (type === "A") {
    const locAisle = getTrimmedFieldValue("bbaisle" + x);
    const locBay = getTrimmedFieldValue("bbbay" + x);
    const locLevel = getTrimmedFieldValue("bblevel" + x);
    const locBin = getTrimmedFieldValue("bbbin" + x);
    const bbValue =
      (locAisle.length ? locAisle + "_" : "") +
      (locBay.length ? locBay + "_" : "") +
      locLevel +
      (locBin.length ? "_" + locBin : "");
    const targetField = getField("bb" + x);
    if (targetField) {
      targetField.value = bbValue.toUpperCase();
    }
  }
  return 1;
}