Velocity / Java脚本表单不保留输入

时间:2017-06-14 19:01:48

标签: javascript velocity

我遇到了在单击“应用”按钮后未返回childDoc值的速度脚本(子文档值消失)。我是Velocity和Javascript的新手。

以下是使用表单的代码部分。

    #set($levelRow = "levelRow")
    #set($newLevelRow = "newLevelRow")
{pre}
<script>
function removeLevel(prefix) {
  var levels = updateCommon(false, prefix);
  var removeRow = document.getElementById(prefix + "levelRow" + levels);
  removeRow.parentNode.removeChild(removeRow);
  if(levels > 2)
    setActionImageVisibility(levels - 1, "visible", prefix);
}
function addLevel(prefix) { 
  var levels = updateCommon(true, prefix);
  var lastLevelRow = document.getElementById(prefix + "levelRow" + levels);
  var newLevelRow = document.getElementById(prefix + "newLevelRow");
  var newLevelInput = newLevelRow.getElementsByTagName("input")[0];
  var toInsertRow = lastLevelRow.cloneNode(true);
  toInsertRow.setAttribute("id", prefix + "levelRow" + (levels + 1));
  var toInsertInputs = toInsertRow.getElementsByTagName("input");
  var toInsertInput = null;
  var toInsertOutput = null; 
  for (var i = 0; i < toInsertInputs.length; i++) {
    var input_id = toInsertInputs[i].getAttribute("id");
    if (input_id == (prefix + "query" + (levels))) {
        toInsertInput = toInsertInputs[i];
    } else if (input_id == (prefix + (levels))) {
        toInsertOutput = toInsertInputs[i];
    }
  }
  toInsertInput.setAttribute("id", prefix + "query" + (levels + 1));
  toInsertInput.value = newLevelInput.value;
  toInsertOutput.setAttribute("id", prefix + (levels + 1));
  toInsertOutput.name = prefix + (levels + 1);
  toInsertOutput.value = toInsertInput.value.replace("/#/", "/redirect/");
  newLevelInput.value = "";
  newLevelRow.parentNode.insertBefore(toInsertRow,newLevelRow);
  setActionImageVisibility(levels, "hidden", prefix)
  setActionImageVisibility(levels + 1, "visible", prefix)
}
function updateCommon(add,prefix) {
  var levelsInput = document.getElementById(prefix + "levelsInput");
  var levels = levelsInput.getAttribute("value") - 1 + 1;
  if(add) {
    levelsInput.setAttribute("value", levels + 1);
  } else {
    levelsInput.setAttribute("value", levels - 1);
  }
  return levels;
}
function setActionImageVisibility(level,visibility,prefix) {
  var img = document.getElementById(prefix + "levelRow" + level).getElementsByTagName("img")[0];
  img.style.visibility = visibility;
}
function submitFunction() {
    var input_prefixes = new Array();
    var docElements = document.getElementsByTagName("input");
    for (docElement in docElements) {
        var indexLevels = docElement.indexOf("levels");
        var levelsAtEnd = (indexLevels + "levels".length) == docElement.length;
        var hasPrefix = docElement.length > "levels".length;
        if ((-1 < indexLevels) && levelsAtEnd && hasPrefix) {
            input_prefixes.push(docElement.substring(0,indexLevels));
            var input_prefix = docElement.substring(0,indexLevels);
            var levelsInput = document.getElementsByName(docElement).item(0);
            for (var i=1; i < parseInt(levelsInput.value) + 1; i++) {
                var input = document.getElementById(input_prefix + "query" + String(i));
                var output = document.getElementsByName(input_prefix + String(i)).item(0);
                output.value = input.value.replace("/#/", "/redirect/");
            }
        }
    }
}
</script>
{/pre}

#macro(loadParameter $id)
  #set($_id = "$id")
  #set($parameter = false)
  #set($parameter = $request.getParameter($_id))
#end

#macro(VariableInputTable $paramBaseName $parametersRedirect)
    #set($parameterPrefix = "${paramBaseName}")

    #loadParameter("${parameterPrefix}levels")
    #set($levels = $parameter)

    #set($requiresParamInit = !$levels)
    #if($requiresParamInit)
        #set($defaultQueries = [""])
        #set($levels = ${defaultQueries.size()})

        #set($initParameters = "${parameterPrefix}levels=${levels}") ## this assumes that it is setting the only parameters on the page
        #foreach($query in $defaultQueries)
            #set($initParameters = "$initParameters&${parameterPrefix}${velocityCount}=$query")
        #end
        #set($parametersRedirect = "${parametersRedirect}${initParameters}")
    #else
        #set($int = 0)
        #set($levels = $int.valueOf($levels))
        #showTable()
    #end
#end

#macro(showTable)
    #set($queries = [])
    #set($linkRoles = [])
    #set($sorts = [])

    <input id="${parameterPrefix}levelsInput" type="hidden" name="${parameterPrefix}levels" value="$levels"></input>
    <table cellpadding="0" border=0>
    #foreach($level in [1..$levels])
        #loadParameter("${parameterPrefix}${level}")
        <tr id="${parameterPrefix}${levelRow}$level">
            <td style="border-width:1px;border-style:solid;padding:1px">
                <input style="border-style:none" id="${parameterPrefix}query$level" type="text" size="50" oninput="submitFunction()" #if($parameter)value="$parameter.replaceAll('"','\\"')#end">
                <input type="hidden" class="polarion-TextBox" name="${parameterPrefix}${level}" id="${parameterPrefix}${level}" style="width: 100%;">
            </td>
            <td  style="vertical-align:middle;text-align:center;">
                <img src="/polarion/ria/images/control/minus.gif" alt="Remove" title="Remove" style="cursor:pointer#if(($velocityCount != $levels) || (2 > $levels));visibility:hidden#end" onclick="removeLevel('${parameterPrefix}')" id="${parameterPrefix}removeImg$level"></img>
            </td>
        </tr>
        #if($parameter)#set($void = $queries.add($parameter))#end
        #set($void = $linkRoles.add([]))
        #set($void = $sorts.add("id"))
    #end
        <tr id="${parameterPrefix}${newLevelRow}">
            <td style="border-width:1px;border-style:solid;padding:1px">
                <input style="border-style:none" id="${parameterPrefix}newQuery" type="text" size="50">
            </td>
            <td style="vertical-align:middle;text-align:center;">
                <img src="/polarion/ria/images/control/plus.gif" alt="Add" title="Add" style="cursor:pointer" onclick="addLevel('${parameterPrefix}')"></img>
            </td>
        </tr>
    </table>
#end

我觉得下面的部分有问题。

  <input style="border-style:none" id="${parameterPrefix}query$level" type="text" size="50" oninput="submitFunction()" #if($parameter)value="$parameter.replaceAll('"','\\"')#end">

  <input type="hidden" class="polarion-TextBox" name="${parameterPrefix}${level}" id="${parameterPrefix}${level}" style="width: 100%;">

表格如下: Form looks like

1 个答案:

答案 0 :(得分:1)

我已设法通过更改代码来解决此问题。

            #if($parameter) #set( $childDocurl = $parameter.replaceAll('"','\\"'))#end


            <input style="border-style:none" id="${parameterPrefix}query$level" type="text" size="50" value="${childDocurl}" oninput="submitFunction()" >
            <input type="hidden" class="polarion-TextBox" name="${parameterPrefix}${level}" id="${parameterPrefix}${level}" value="${childDocurl}" style="width: 100%;">