将 JSP 变量输出到 JavaScript 代码段中

Output JSP variable into JavaScript snippet

本文关键字:JavaScript 代码 段中 输出 JSP 变量      更新时间:2023-09-26

我的循环中有一个varStatus,我想将其发送到js中的函数。

这是我所拥有的:

onkeyup="clickableButton(this.value, ${newCommentStatus.index})"

${newCommentStatus.index}是不确定的。我如何发送此值?

提前致谢

编辑

    <c:forEach items="${threadviewModel.emailToShowList}" var="row" varStatus="status">
    <form:form modelAttribute="commentFieldModel">
<span class="commentInput"><form:input id="commentInput" type="text" path="commentText" size="90" value=" " onkeyup="clickableButton(this.value, ${status.index})"/></span>
                                        <div><input disabled="true" type="submit" id="save-${status.index}" name="_eventId_addComment" value="Save"/></div>
                                    </div>
                            </form:form>

脚本代码

function clickableButton(text, id) {
            if (text.length > 0)
                document.getElementById("save-" + id).disabled = false;
            else
                document.getElementById("save-" + id).disabled = true;
        }
        ;

您的<c:forEach>调用变量"status",而不是"newCommentStatus"。

事件处理程序如下所示:

... onkeyup="clickableButton(this.value, ${status.index})" ...

另外,你可以清理你的JavaScript:

    function clickableButton(text, id) {
        document.getElementById("save-" + id).disabled = text.length <= 0;
    }

试试这个方法

onkeyup="clickableButton(this.value, '${newCommentStatus.index}')"

${newCommentStatus.index} 不是标准的 JavaScript。
我怀疑您正在使用JQuery或类似的东西:尝试${newCommentStatus}.index
这将适用于一些假设。