如何发布/提交禁用的输入复选框?
我有一个复选框,在特定条件下需要禁用该复选框。结果 HTTP 不会发布禁用的输入。
我该如何解决这个问题?即使输入被禁用也提交输入并保持输入禁用?
I have a checkbox that, given certain conditions, needs to be disabled. Turns out HTTP doesn't post disabled inputs.
How can I get around that? submitting the input even if it's disabled and keeping the input disabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
更新:
READONLY
不适用于复选框您可以使用
disabled="disabled"
,但此时复选框的值不会出现在POST
值。其中一种策略是在同一表单中添加一个隐藏字段来保存复选框的值,并从该字段读取值只需将disabled
更改为readonly
UPDATE:
READONLY
doesn't work on checkboxesYou could use
disabled="disabled"
but at this point checkbox's value will not appear intoPOST
values. One of the strategy is to add an hidden field holding checkbox's value within the same form and read value back from that fieldSimply changedisabled
toreadonly
我已经解决了这个问题。
由于
readonly="readonly"
标签不起作用(我尝试过不同的浏览器),因此您必须使用disabled="disabled"
反而。但是您的复选框的值将不会发布...
这是我的解决方案:
要同时获得“readonly”外观和POST复选框的值,只需添加与您的复选框具有相同名称和值的隐藏“输入”。您必须将其放在复选框旁边或相同的
标记之间:
此外,只是为了让您知道
readonly="readonly", readonly ="true"、readonly=""、
或只是READONLY
无法解决此问题!我花了几个小时才弄清楚!该参考文献也不相关(可能还没有,或者不再相关,我不知道):
http://www.w3schools.com/tags/att_input_readonly.asp
I've solved that problem.
Since
readonly="readonly"
tag is not working (I've tried different browsers), you have to usedisabled="disabled"
instead. But your checkbox's value will not post then...
Here is my solution:
To get "readonly" look and POST checkbox's value in the same time, just add a hidden "input" with the same name and value as your checkbox. You have to keep it next to your checkbox or between the same
<form></form>
tags:Also, just to let ya'll know
readonly="readonly", readonly="true", readonly="",
or justREADONLY
will NOT solve this! I've spent few hours to figure it out!This reference is also NOT relevant (may be not yet, or not anymore, I have no idea):
http://www.w3schools.com/tags/att_input_readonly.asp
如果您喜欢使用 JQuery,请在提交表单时删除禁用属性:
If you're happy using JQuery then remove the disabled attribute when submitting the form:
创建一个 css 类,例如:
应用该类而不是禁用属性
create a css class eg:
apply this class instead of disabled attribute
您可以这样处理...对于每个复选框,创建一个具有相同
name
属性的隐藏字段。但是,请使用一些您可以测试的默认值来设置该隐藏字段的值。例如..如果提交表单时复选框被“选中”,则该表单参数的值将为
如果未选中复选框,则该值将为
您可以使用任何值而不是“false”,但是你明白了。
You could handle it this way... For each checkbox, create a hidden field with the same
name
attribute. But set the value of that hidden field with some default value that you could test against. For example..If the checkbox is "checked" when the form is submitted, then the value of that form parameter will be
If the checkbox is not checked, then the value would be
You could use any value instead of "false", but you get the idea.
最简单的方法是:
这将阻止用户取消选择此复选框,并且在提交表单时仍会提交其值。如果您希望用户无法选择此复选框,请删除选中的复选框。
另外,一旦满足特定条件(如果这就是您所追求的),您可以使用 javascript 清除 onclick。这是一个简陋的小提琴,展示了我的意思。它并不完美,但你已经明白了要点。
http://jsfiddle.net/vwUUc/
The simplest way to this is:
This will prevent the user from being able to deselect this checkbox and it will still submit its value when the form is submitted. Remove checked if you want the user to not be able to select this checkbox.
Also, you can then use javascript to clear the onclick once a certain condition has been met (if that's what you're after). Here's a down-and-dirty fiddle showing what I mean. It's not perfect, but you get the gist.
http://jsfiddle.net/vwUUc/
我从问题开始:“如何发布/提交禁用的输入复选框?”在我的回答中,我跳过了评论:“如果我们想禁用一个复选框,我们肯定需要保留一个前缀值(选中或未选中),并且我们希望用户意识到它(否则我们应该使用隐藏类型和不是复选框)”。在我的回答中,我认为我们希望始终选中一个复选框,并且代码以这种方式工作。如果我们单击该复选框,它将永远被选中,并且其值将被发布/提交!同样的,如果我写
onclick="this.checked=false" 没有checked="checked" (注意:默认为未选中)它将永远处于未选中状态,并且其值将不会被发布/提交!。
I started from the problem: "how to POST/Submit an Input Checkbox that is disabled?" and in my answer I skipped the comment: "If we want to disable a checkbox we surely need to keep a prefixed value (checked or unchecked) and also we want that the user be aware of it (otherwise we should use a hidden type and not a checkbox)". In my answer I supposed that we want to keep always a checkbox checked and that code works in this way. If we click on that ckeckbox it will be forever checked and its value will be POSTED/Submitted! In the same way if I write
onclick="this.checked=false" without checked="checked" (note: default is unchecked) it will be forever unchecked and its value will be not POSTED/Submitted!.
不要禁用它;相反,将其设置为只读,尽管这没有任何效果,因为不允许用户更改状态。但是您可以使用 jQuery 来强制执行此操作(防止用户更改复选框的状态:
这假设您不想修改的所有复选框都具有“只读”属性。例如
Don't disable it; instead set it to readonly, though this has no effect as per not allowing the user to change the state. But you can use jQuery to enforce this (prevent the user from changing the state of the checkbox:
This assumes that all the checkboxes you don't want to be modified have the "readonly" attribute. eg.
这就像一个魅力:
唯一的缺点是提交时禁用的输入字段会短暂闪烁。至少在我的场景中这不是什么大问题!
This works like a charm:
The only disadvantage is the short flashing up of the disabled input fields when submitting. At least in my scenario that isn´t much of a problem!
因为:
我将为您提供另一个可能性:
将您的复选框设置为正常禁用。在用户提交表单之前,通过 JavaScript 启用此复选框。
由于该复选框在提交表单之前启用,因此其值以常见方式发布。该解决方案唯一不太令人愉快的是该复选框会启用一段时间并且用户可以看到。但这只是我可以忍受的一个细节。
Since:
I'm gonna offer you another possibility:
Set your checkbox as disabled as normal. And before the form is submitted by user enable this checkbox by JavaScript.
Because the checkbox is enabled before the form is submitted, its value is posted in common way. Only thing that isn't very pleasant about this solution is that this checkbox becomes enabled for a while and that can be seen by users. But it's just a detail I can live with.
除了
隐藏
字段之外没有其他选项,因此请尝试使用隐藏
字段,如下面的代码所示:There is no other option other than a
hidden
field, so try to use ahidden
field like demonstrated in the code below:不幸的是,没有“简单”的解决方案。 readonly 属性不能应用于复选框。我阅读了有关复选框的 W3 规范,发现了罪魁祸首:
如果提交表单时控件没有当前值,则用户代理不需要将其视为成功的控件。( http://www.w3.org/TR/html401 /interact/forms.html#h-17.13.2)
这会导致未选中状态和禁用状态产生相同的解析值。
唯一完全证明的方法是在提交表单时使用 javascript 添加同名的隐藏输入。
您可以使用我为此制作的小片段:
这会查找文档中的第一个表单,收集所有输入并搜索禁用的输入。当找到禁用的输入时,会在父节点中创建一个具有相同名称和值“on”(如果其状态为“已选中”)和“”(如果其状态为“” - 未选中)的隐藏输入。
该函数应由 FORM 元素中的事件“onsubmit”触发,如下所示:
Unfortunately there is no 'simple' solution. The attribute readonly cannot be applied to checkboxes. I read the W3 spec about the checkbox and found the culprit:
If a control doesn't have a current value when the form is submitted, user agents are not required to treat it as a successful control. (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2)
This causes the unchecked state and the disabled state to result in the same parsed value.
The only full-proof way to do this is to add a hidden input with the same name with javascript on submitting the form.
You can use the small snippet I made for this:
This looks for the first form in the document, gathers all inputs and searches for disabled inputs. When a disabled input is found, a hidden input is created in the parentNode with the same name and the value 'on' (if it's state is 'checked') and '' (if it's state is '' - not checked).
This function should be triggered by the event 'onsubmit' in the FORM element as:
您可以根据需要将其保持禁用状态,然后在提交表单之前删除禁用属性。
You can keep it disabled as desired, and then remove the disabled attribute before the form is submitted.
将
onsubmit="this['*nameofyourcheckbox*'].disabled=false"
添加到表单中。Add
onsubmit="this['*nameofyourcheckbox*'].disabled=false"
to the form.添加
onclick="this.checked=true"
解决我的问题:例子:
Adding
onclick="this.checked=true"
Solve my problem:Example:
我只是在此处的答案中结合了 HTML、JS 和 CSS 建议
HTML:
jQuery:
CSS:
由于该元素没有“禁用”,它仍然会通过 POST。
I just combined the HTML, JS and CSS suggestions in the answers here
HTML:
jQuery:
CSS:
Since the element is not 'disabled' it still goes through the POST.
函数 some_name 是前一个子句的示例,用于根据子句管理第二个复选框,该复选框已选中(发布其值)或未选中(不发布其值),并且用户无法修改其状态;无需管理第二个复选框的任何禁用
Function some_name is an example of a previous clause to manage the second checkbox which is checked (posts its value) or unchecked (does not post its value) according to the clause and the user cannot modify its status; there is no need to manage any disabling of second checkbox
这是可以从后端控制的另一种解决方法。
ASPX
在 ASPX.CS
类中仅出于样式目的而添加。
Here is another work around can be controlled from the backend.
ASPX
In ASPX.CS
Class is only added for style purposes.