【发布时间】:2013-08-26 19:43:46
【问题描述】:
由于某种原因我的代码不起作用?!我需要选中复选框,如果所有复选框都“未选中”,则启用隐藏字段并将值发送到 db,这样我就可以确定复选框未选中。
问题 1:为什么我的 jquery 代码不起作用?
问题 2: 我需要将选中或未选中的值记录到 DB。我的计划是检查是否没有在 php.ini 中选择。这是一个好习惯吗?
HTML
<tr class="more_info">
<th valign="top">Other options:</th>
{foreach from=$HTML_cOPTIONS key=dbname item=optname}
{if ! empty($CARINFO[car].{$dbname})}
<td><input type="checkbox" id="forced_checkbox" name="c_options[]" value="{$dbname}" checked/> {$optname} </br></td>
{else}
<td><input type="checkbox" id="forced_checkbox" name="c_options[]" value="{$dbname}"/> {$optname} </br></td>
{/if}
{/foreach}
<input type="hidden" id="forceSendCheckBox" name="c_options[]" value="nocheckboxes"/>
<input type="button" value="Check" id="check" />
</tr>
jQuery
$('#check').bind('click',function()
{
if ($('#forced_checkbox').filter(':not(:checked)').length == 0) {
console.log('at least one checked');
$("#forceSendCheckBox").prop('disabled',true);//do not send this field to DB
} else {
console.log('nothing checked');
$("#forceSendCheckBox").prop('disabled',false);//send empty checkboxes
}
});
更新
这是一个小提琴,它似乎仍然坏了。 :-/
【问题讨论】:
-
为什么你的标题说你需要检查是否所有都被选中,而你的描述却说你需要询问是否所有都未选中?
-
你没有元素
id="check"。 -
@crazy train,这是一个错字。您有超过 2000 个代表,并且无法编辑错字?!你花了更长的时间才发表评论。
-
@Timur:我不知道你想要哪个。大多数人会感谢你指出矛盾。归根结底,你的问题是你的责任,不是我的。
-
...您知道,此代码:
.filter(':not(:checked)').length == 0与此消息相结合:console.log('at least one checked'),这看起来很奇怪。如果有0未选中的框,那么会声明"all are checked",而不是"at least one checked"。因此,如果您想查看是否所有内容都“未选中”,那么您应该执行.filter(":checked").length == 0。或者如果您想知道是否至少检查了一个,那么:.filter(":checked").length
标签: php javascript jquery checkbox smarty