简体   繁体   English

Spring表单更改了值,未反映在ModelAttribute中

[英]Spring form changed value in not getting reflected in ModelAttribute

I have the following form 我有以下表格

<form:form id="form" name="form" target="dummyHiddenForm"  method="post" commandName="myForm" >

            <span class="container">
                <label>TestKey</label>
                <span class="container-right">
                    <form:input path="key" id="key" cssClass="text" maxlength="200" cssStyle="width:60%" disabled="true"/>
                    <input type="button" class="refreshIcon" style="width:auto;" onclick="javascript:refreshKey()" />
                </span>
            </span>

    <span style="width:auto; padding-left: 30%; padding-bottom: 4%; text-align:center; float:right; clear:both;">
        <input type="button" class="button" style="width:auto;" value="Submit" onclick="javascript:submitForm()"/>
    </span> 

</form:form>

<iframe id="dummyHiddenForm" name="dummyHiddenForm" style="display:none;" ></iframe>

In GET request for above page containing form I am setting some default value for each variable of object myForm and key is set to value "" . 在包含表单的上一页的GET请求中,我为对象myForm的每个变量设置了一些默认值,并且key设置为值"" and my java script function for refreshKey() is 我的refreshKey()的java脚本函数是

function refreshKey(){
    document.getElementById('key').value = generateKey() ; 
}

This method correctly works and onclick sets new key in the input text fields. 此方法可以正常工作,并且onclick在输入文本字段中设置了新键。

on click of submit button I am submitting the form using java script 在单击提交按钮时,我正在使用Java脚本提交表单

function submitForm(){
        var formVar = document.forms["form"];
        formVar.action = "/test.htm";
        formVar.submit();
}

But in my controller I get the same default value "" for key irrespective of the the actual value of the input field. 但是在我的控制器中,无论输入字段的实际值是多少,我都会为键获得相同的默认值""

I tried using javascript alert and .value I can see the changed value but the value does not change in actual DOM (at least when I do inspect element from firefox it still shows value="" ). 我尝试使用javascript alert.value我可以看到更改后的值,但该值在实际DOM中没有更改(至少当我从firefox中检查元素时,它仍然显示value="" )。

I need to get the changed value when the form is submitted. 提交表单时,我需要获取更改后的值。 Any suggestions what is going wrong? 有什么建议怎么回事?

Figured out an workaround. 找出解决方法。 The problem was arising due to disabled property ie disabled="true" . 该问题是由于禁用的属性引起的,即disabled="true" Though java script has acess and can modify the value attribute of the input it is not reflected when the spring form is submitted. 尽管Java脚本具有访问权限,并且可以修改输入的value属性,但是提交spring表单时,它不会反映出来。 There was no problem with model attribute binding which I suspected in the begining. 我一开始就怀疑模型属性绑定没有问题。 Works fine with the disabled attribue turned off. 在禁用的属性关闭的情况下工作正常。

However if you still want to make your input field uneditable there is another property readonly which you can set to true. 但是,如果你仍然想使你的输入字段不可编辑的还有另一种属性readonly ,你可以设置为true。 Change the value with javascript and the value is reflected back in the model attribute on form submit. 使用javascript更改值,该值会反映在表单提交的model属性中。

<form:input path="key" id="key" cssClass="text" maxlength="200" cssStyle="width:60%" readonly="true"/>

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

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