CakePHP 在 SELECT 输入中选择默认值

CakePHP select default value in SELECT input(CakePHP 在 SELECT 输入中选择默认值)
本文介绍了CakePHP 在 SELECT 输入中选择默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用 CakePHP:

Using CakePHP:

我有一个多对一的关系,让我们假设它是许多叶子到树.当然,我烘焙了一个表单,将一个叶子添加到一棵树中,您可以通过表单助手创建的下拉框(标签)指定它是哪个树.

I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.

唯一的问题是,SELECT 框始终默认为 Tree #1,但我希望它默认为要添加到的 Tree:

The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:

例如,调用 example.com/leaf/add/5 会调出界面,将新的叶子添加到树 #5.Leaf.tree_id 的下拉框将默认为Tree 5",而不是当前默认为Tree 1".

For example, calling example.com/leaf/add/5 would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id would default to "Tree 5", instead of "Tree 1" that it currently defaults to.

我需要在我的 Leaf 控制器和 Leaf view/add.ctp 中放入什么来做到这一点?

What do I need to put in my Leaf controller and Leaf view/add.ctp to do this?

推荐答案

你不应该使用 select(), or text(), or radio() 等;这是可怕的做法.你应该使用 input():

You should never use select(), or text(), or radio() etc.; it's terrible practice. You should use input():

$form->input('tree_id', array('options' => $trees));

然后在控制器中:

$this->data['Leaf']['tree_id'] = $id;

这篇关于CakePHP 在 SELECT 输入中选择默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)