我有一种情况,我想允许将表单变量设置为一组值之一(在我当前的情况下为真/假)或未设置(在这种情况下不返回任何值,而不是返回一些“无”值或一个空白)。复选框可以给出未设置的位,但只能给出一个设置值。无线电元件可以起到某种作用。但是一旦选择了某个值,就无法返回取消设置。无论如何,我看过的所有其他输入总是设置变量。
我是否遗漏了什么,或者我是否不得不接受一个不太理想的解决方案?
I have a case where I want to allow a form variable to be set to one of a set of value (in my current case true/false) or left unset (in which case no value is returned rather than some 'none' value or a blank). A check box can give the unset bit but only one set value. A radio element could work, sort of. But once a value is selected there is no way to go back to unset. All the other inputs I've looked at always set the variable no matter what.
Am I missing something or am I just going to have to accept a less-than-ideal solution?
发布评论
评论(7)
三个单选按钮或带有三个
的
Three radio buttons or a
<select>
with three<option>
will do.放置三个收音机:A、B 和 None
Put three radios: A, B and None
执行“下拉菜单”,如下所示: http://www.echoecho.com/htmlforms11.htm< /a>
创建默认值“None”,后跟选项 A,然后是选项 B。
Do a 'drop down menu' as show here: http://www.echoecho.com/htmlforms11.htm
Create the default value as 'None' followed by option A and then option B.
下拉列表怎么样?
我最近一直在网上申请很多工作,这是人们通常采取的路线。
What about a drop down list with
I've been applying to a lot of jobs online lately, and this is the route people generally have been taking.
下拉框有用吗?
Would a dropdown box work?
您需要使用一些 JavaScript。如果你有 jQuery,那么有几个三态复选框插件。
例如: http://plugins.jquery.com/project/tristate-checkbox
那里如果您在 google 中搜索“三态复选框”,则也可能是非 jQuery 脚本,
例如: http://caih.org/open-source-software/tri-state-checkbox-in-javascript/
You would need to use some javascript. If you have jQuery there are several tri-state checkbox plugins.
For example: http://plugins.jquery.com/project/tristate-checkbox
There are also probably non jQuery scripts if you google for "tri state checkbox"
For example: http://caih.org/open-source-software/tri-state-checkbox-in-javascript/
在一组中使用 3 个单选按钮,您可以使用 CSS 隐藏第一个“无”单选按钮(
visibility:hidden;
或display:none;
),如果这是在表单验证期间仍然选择了一个,则用户没有选择 true 或 false radio。编辑(发表评论):
如果不需要Javascript,那么您可以有条件地在隐藏无线电上应用“hideableItem”类,如果禁用脚本,您将得到的最坏情况是3个无线电供用户选择,如下所示其他人已经描述过。如果启用了 JS,则默认无线电将被隐藏并提供我所描述的行为。
这里描述了如果 JS 被禁止/启用的技术的条件隐藏: http://lucassmith.name/2008/10/script-to-add-css-only-when-javascript-is-available.html
我一直使用它,它伟大的。
Using 3 radio buttons in one group, you could hide the first 'none' radio button with CSS (
visibility:hidden;
ordisplay:none;
), and if that's the one still selected during your form validation, then the user hasn't chosen either of the true or false radios.EDIT (post comments):
If no-Javascript is a requirement, then you can conditionally apply a 'hideableItem' class on the hidden radio, if scripting is disabled the worst you'll get is 3 radio for the user to choose from, as others have described. If JS is enabled, then the default radio is hidden and provides the behaviour i've described.
The conditional hiding if JS is dis/enabled technique is described here: http://lucassmith.name/2008/10/script-to-add-css-only-when-javascript-is-available.html
I use it all the time, its great.