如何将函数从vbscripts转换为javascript

时间:2019-06-12 10:43:47

标签: javascript vbscript

我有一个vbscript函数,我需要隐瞒javascript函数。该函数使用eval(),这使我难以理解该部分,从而使我很困惑

由于当前功能在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

这是我在javascript中想到的,但是我怀疑如果我将其复制用于其他变量,这是否可以工作。

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

1 个答案:

答案 0 :(得分:2)

类似的方法可能对您有用,但是如果没有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;
}