简体   繁体   English

在 Struts 2 中未使用 JavaScript 函数设置 readOnly 属性

[英]readOnly attribute not being set with JavaScript function in Struts 2

I am trying to create an edit link such that when it is clicked it opens the details for that row in read-only mode.我正在尝试创建一个编辑链接,以便在单击它时以只读模式打开该行的详细信息。

Here is the link:链接在这里:

<c:set var="deletableBook" value="0"/>

<a href="" title="Edit Book Info" onClick='resetDateAndMakeReadOnly(${deletableBook}); return performAction(${item.bookId}, "bookEdit");'>Edit</a>

And here's the function that gets called:这是被调用的函数:

 function resetDateAndMakeReadOnly(allEditable) { var e22 = document.getElementById('book_Date'); var e3 = document.getElementById('book_type'); var e4 = document.getElementById('book_Number'); if (allEditable){ e22.readOnly=false; e3.disabled=false; e4.readOnly=false; alert("read and write"); } else { e22.readOnly=true; e3.disabled=true; e4.readOnly=true; alert("readOnly new"); } e22.value = "<c:out value='${params.book_Date}'/>"; return false; }

And currently nothing seems to change when this method is run.目前,运行此方法时似乎没有任何变化。 I've confirmed that it makes it to the correct part of the logic, but things are still editable.我已经确认它使它成为逻辑的正确部分,但事情仍然是可编辑的。

It is because you are using link with empty href to trigger your javascript function which will reload your page.这是因为您正在使用带有空href链接来触发您的 javascript 函数,该函数将重新加载您的页面。 Use javascript:void(0);使用javascript:void(0); inside href .里面href

<a href="javascript:void(0);" title="Edit Book Info" onClick='resetDateAndMakeReadOnly(${deletableBook}); return performAction(${item.bookId}, "bookEdit");'>Edit</a>

The attribute deletableBook is not a boolean value, and is not false that you expect in the javascript function.属性deletableBook不是布尔值,也不是您在 javascript 函数中期望的false To switch the variable in the action在动作中切换变量

session.put("allEditable", !(session.get("allEditable")==null?Boolean.FALSE:(Boolean)session.get("allEditable")));

then use然后使用

$(document).ready(function(){
    resetDateAndMakeReadOnly(<s:property value="%{#session.allEditable}"/>);
});

that will reset fields attributes depending on allEditable when the page is reloaded.这将在重新加载页面时根据allEditable重置字段属性。 But this但是这个

<s:a href="" title="Edit Book Info" onClick="resetDateAndMakeReadOnly(%{#session.allEditable});">Make readonly</s:a>

will not reload the page and keep the value that you have in the allEditable session attribute.不会重新加载页面并保留您在allEditable会话属性中的值。 That syntax may be a little bit confuse the IDE but is correctly evaluate the OGNL expression and renders like该语法可能有点混淆 IDE,但正确评估 OGNL 表达式并呈现如下

<a title="Edit Book Info" onClick="resetDateAndMakeReadOnly(false);">Make readonly</a>

there's no href attribute, that's why the page is not reloaded.没有href属性,这就是页面不会重新加载的原因。

Also elements in the JSP should be findable by their id attribute should be set.此外,应该设置 JSP 中的元素可以通过它们的id属性找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM