php Codeigniter set_checkbox() 保持选中状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16437136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter set_checkbox() keeping checked state
提问by Andrej
I have this code in my view:
我在我看来有这个代码:
<input type="checkbox" name="test[]" value="1" <?php echo set_checkbox("test[]", "1") ?> >test1
<input type="checkbox" name="test[]" value="2" <?php echo set_checkbox("test[]", "2") ?> >test2
These checkboxes are not required, but some other fields on the form are. I want to keep the state of these checkboxes when I post the form. The values from the checkboxes are in the POST if they are checked, but the checked state is not kept when the form loads again after the post (if some of them is checked before the post, I want it to be checked after the post). I also tried set_checkbox(“test”, “1”), but it didn't work. The values on the other text fields is kept using the set_value() function and is working fine.
这些复选框不是必需的,但表单上的其他一些字段是必需的。我想在发布表单时保留这些复选框的状态。复选框中的值如果被选中则在 POST 中,但在发布后再次加载表单时不会保持选中状态(如果其中一些在发布前被选中,我希望它在发布后被选中) . 我也试过 set_checkbox(“test”, “1”),但是没有用。其他文本字段上的值使用 set_value() 函数保留并且工作正常。
回答by evannah
I realised that set_checkbox takes 3 parameters :
我意识到 set_checkbox 需要 3 个参数:
set_checkbox(string $checkboxname, string $value, boolean $isChecked);
For example :
例如 :
echo form_checkbox('mycbx[]',
$item['id'],
set_checkbox('mycbx[]', $item['id'], false)
);
or this way :
或者这样:
$checkbox = array(
'name' => 'mycbx[]',
'value' => $item['id'],
'checked' => set_checkbox('mycbx[]', $item['id'], false)
);
echo form_checkbox($checkbox);
回答by Maximus
In your view add a line like this:
在您的视图中添加如下一行:
<input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1', true); ?> />
The above code assumes that you want the box checked by default, and if not then just change the 3rd parameter to false.
上面的代码假设您希望默认情况下选中该框,如果没有,则只需将第三个参数更改为 false。
Then in your validation add a line with no rules(so codeigniter knows it has to do something with it). Here's an example:
然后在你的验证中添加一行没有规则(所以 codeigniter 知道它必须用它做一些事情)。下面是一个例子:
$this->load->library('form_validation');
$this->form_validation->set_rules('mycheck[]', 'My message that no one will ever see.', '');
Lastly, make sure that you DON'T explicitly set the input item as checked via the input state. That will override the codeigniter parameter. In otherwords DON'T do this <input checked />
.
最后,请确保您没有通过输入状态将输入项显式设置为已检查。这将覆盖 codeigniter 参数。换句话说,不要这样做<input checked />
。
And that's it. Should work like charm.
就是这样。应该像魅力一样工作。
回答by Rajesh RK
set_checkbox('mycheckbox', 'value',TRUE)
set_checkbox('mycheckbox', 'value',TRUE)
回答by karmafunk
Remove the [] from the set_checkbox method call.
从 set_checkbox 方法调用中删除 []。
For a more informed answer see Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation
有关更明智的答案,请参阅表单验证失败后重新填充 Codeigniter 中的复选框
回答by Nikunj Dhimar
When you're using an array like "test[]" in form, you don't need to include the square brackets ([]) in your set_checkbox
call.
当您在表单中使用像“test[]”这样的数组时,您不需要在set_checkbox
调用中包含方括号 ([]) 。
The set_checkbox should always be like this:
set_checkbox 应该始终是这样的:
set_checkbox('test', 'value');
Where 'value' is the second parameter of the form checkbox.
其中“值”是表单复选框的第二个参数。