0

我可以从 PHP 生成我的表单字段:

<?php
    echo $this->Form->input('Model.0.name', array(
            'label' => __('Name')
        )
    );
?>

但是,需要“即时”创建字段,例如当用户单击“新名称字段”时。我不知道如何开始。。

4

1 回答 1

2

我不知道 cakephp 如何工作,但假设它与 Controllers->functions (site.com/controller/function) 一起工作,你可以这样做:

<?

class FieldCreator extends Controller{

    public function input()
    {
        echo $this->Form->input('Model.0.name', array(
            'label' => __('%name%')
            )
        );
    }   
}

jQuery

Jquery 提供了一个get函数来发出 GET 请求,就像FieldCreator控制器一样。假设您的视图如下所示:

<html>
    <head>
            <!-- import jquery framewrok -->
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    </head>
    <body>

            <!-- This link will act like a button -->
        <a href="#" class="new_input">New name field<a>

        <form id="my_form">

        </form>

            <!-- Prepare the script once the page is ready -->
        <script type="text/javascript">
            $(document).ready(function(){  

                            var content = '';
                            var index = 1;
                            // Use click event in the link above to trigger the request
                $('.new_input').click(function(){ 

                                    if (content == '')
                                    // Do the request to site.com/FieldCreator/input
                    $.get('site.com/FieldCreator/input', function(data){ 
                            content = data;
                    });  

                                   //create the input
                                  createInput('name_' + index);
                });
            });

                    function createInput(name)
                    {
                   var input = content.replace('%name%', name);
                   $('#my_form').append(input);
                       index ++;
                     }




        </script>

    </body>
</html>

但是,如果您想重新利用 GET 结果,您可以:

  1. 更改'label' => __('Name')'label' => __('%name%')
于 2012-10-01T18:43:14.067 回答