在Cakephp中添加和删除表单字段 [英] Add and remove form fields in Cakephp

查看:189
本文介绍了在Cakephp中添加和删除表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法在cakephp有一个表单,用户可以在提交之前添加和删除表单字段,看看四周,并询问蛋糕IRC的答案似乎是使用Jquery,但经过几个小时的查看

Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.

我在蛋糕中找到的一个例子是 - http://www.mail-archive.com/cake-php@googlegroups.com/msg61061.html 但我最好的努力后,我不能让这段代码工作正常(我认为它的调用控制器/模型,未列出的例子)

The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php@googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)

我还发现一个直接jquery示例(http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/),它做什么,我想我的形式,但我不能解决如何使用cakephp形式助手与它,以使它正常工作,并得到命名正确。 (显然$ form helper是php所以我不能生成任何东西,浏览器加载后)。

I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).

我是一个新的蛋糕,从来没有使用过jQuery和我绝对勉强如何做到这一点,如果任何人有一个cakephp的例子,他们有工作,或可以指出我的正确方向,我需要完成这一点,将非常感激。

I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.

提前感谢

推荐答案

我会采取直接的jquery路线,个人。我想你可以让PHP生成代码为jquery插入(这样你可以使用形式助手),但它增加了复杂性,没有任何东西。

I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.

帮助器只是生成html,看看你想生成的html。假设你想要一些添加另一个字段,当点击时,将在html中添加另一个字段。要添加的HTML将类似于:

Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:

<input type="text" name="data[User][field][0]" />



现在,要使用jquery插入它,我会做一些事情,

Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.

    $(document).ready( function() {
      $("#link_id").click( 'add_field' );
      var field_count = 1;
    } );

    function add_field()
    {
      var f = $("#div_addfield");
      f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
      field_count++;
    }

当然,如果用户不提交并返回此页面,他们失去了进步,但我认为这是关于你想要完成的基础。

Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.

这篇关于在Cakephp中添加和删除表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆