jQuery 如何将 Spring 表单设为只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15875301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:56:45  来源:igfitidea点击:

How to Make Spring form as Read Only?

javascriptjqueryspringjsp

提问by Java Developer

In my Application i'm using Spring Supplied tags...

在我的应用程序中,我使用的是 Spring Supplied 标签...

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

    <fieldset id="user1">
       <form:form action="frm1" modelAttribute="SUPER" >
         ------
         ------
       </form:form>
    </fieldset>

And My Requirement is basically Form is only Read only when we click Edit button of the form then only form will change Editable mode.

而我的要求基本上是表单只有只读,当我们点击表单的编辑按钮时,只有表单会改变可编辑模式。

I'm trying readonlyProperty but not work .. i think this property for html.

我正在尝试readonly属性但不起作用.. 我认为此属性适用于 html。

Please give me suggestion how to do this in any java related technology.. Like javascript and JQuery.

请给我建议如何在任何与 Java 相关的技术中执行此操作.. 像 javascript 和 JQuery。

采纳答案by Sachin Gorade

You can use c tag libraryto assign the values to your inputs from your model while using readonly="readonly" property.

您可以使用c 标记库在使用 readonly="readonly" 属性时将值分配给模型的输入。

<form:form id="simpleDomainForm" modelAttribute="simpleDomain">
<input name="modelId" value='<c:out value="${simpleDomain.modelId}"></c:out>' readonly="readonly" class="required">
<input name="modelName" value='<c:out value="${simpleDomain.modelName}"></c:out>' readonly="readonly" class="required">
<input type="button" value="Toggle" onclick="toggleForm();">
</form:form>

And jQuery you can enable these input boxes on click event of Editbutton. Following is a simple example of script using jQuery to toggle readonly state of inputs.

和 jQuery 您可以在编辑按钮的单击事件上启用这些输入框。以下是使用 jQuery 切换输入的只读状态的脚本的简单示例。

[Note: This is just a simple example, you need to customize it according to your requirement]

[注意:这只是一个简单的例子,您需要根据您的要求进行自定义]

<script>
function toggleForm(){
    if($("#simpleDomainForm").children(".required").attr("readonly") == "readonly")
        $("#simpleDomainForm").children(".required").removeAttr("readonly");
    else
        $("#simpleDomainForm").children(".required").attr("readonly", "readonly");
}
</script>

回答by Vineet Sharma

<form:input path="firstName" class="form-control"
            placeholder="First Name"  type="text" readonly="true"/>