我有一个多对多的关系,由后端表单中的复选框表示。创建新记录时,如何设置表单以选中所有复选框?
问问题
267 次
1 回答
0
嗯,不确定我们能否以正确的方式做到这一点,因为实际上要选中该复选框,我们需要 DB 中的实际关系,但当时我们没有这样的关系,但我们可以为它做一些修改
好的,首先如果您使用表单生成器创建表单,you have controller and its view files
现在去它的视图文件找到create.htm
现在找到这一行<?php Block::endPut() ?>
并粘贴 this code after
。像这样
<?php Block::endPut() ?>
/* just replace User[groups][] with your field name */
/* here User => your model name first latter caps */
/* [groups] => your relation name all small */
/* or you can inspect it and paste it here using devtools */
<script>
jQuery(document).ready(function(){
jQuery("[name='User[groups][]']").attr('checked', true)
/* replace here ^ */
})
</script>
这些代码和平将在页面加载时选中所有框。
它不是最好的方法,一种黑客,因为我没有找到任何好的简单替代方案。
如果您发现任何困难,请发表评论。
于 2018-04-02T09:50:52.917 回答