简体   繁体   English

如何将禁用的元素值发送到服务器?

[英]How to send the disabled element value to server?

I have the next checkbox: 我有下一个复选框:

<input type="checkbox" value="true" name="permanentEmployee" checked="" id="permanentEmployee" disabled="">

I've set the attribute disabled to true and submitted the form 我给自己定的属性disabled为true,并提交表单

$("#permanentEmployee").prop('disabled', true);`

On server side, if I do: 在服务器端,如果我这样做:

request.getParameter("permanentEmployee")

I get the value as Null , though I get the the correct value (ie True ) if I don't disable the checkbox. 我得到的值为Null ,但是如果不禁用复选框,则会得到正确的值(即True )。 Why am I not getting the disabled checkbox value as True even if it is checked? 为什么即使选中了禁用复选框值也无法将其设置为True

Disabled form elements are not sent via form submit. 禁用的表单元素不会通过表单提交发送。 You can consider using read-only instead 您可以考虑改用只读

One idea would be 一个想法是

$('#permanentEmployee').click(function(){
        return false; // even if the user clicks on it it will not change
});

$('#permanentEmployee').prop('checked', true);

http://jsfiddle.net/Spokey/M3LfA/1/ http://jsfiddle.net/Spokey/M3LfA/1/

在提交表单(提交提交侦听器)之前,您可以启用该复选框。

Disabled check boxes (and probably other form elements) are not sent to the server. 禁用的复选框(可能还有其他表单元素)不会发送到服务器。

There are a couple of solutions here: How to ensure a <select> form field is submitted when it is disabled? 这里有两个解决方案: 如何确保禁用<select>表单字段时提交?

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

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