Jquery在编辑表单上添加了表单字段

时间:2014-05-02 20:57:29

标签: jquery cakephp

我在add.ctp的表单中有一些用Jquery插入的动态字段。这工作正常,数据存储在数据库中。但问题在于edit.ctp中的编辑表单。因为动态添加的字段不会填充数据库中的数据。我使用CakePHP 2.4.1和Jquery 1.7.2。所以我的问题是如何使用数据库中的相关数据填充edit.ctp中的表用户?我找不到任何相关的教程。请问你能帮帮我吗?我的代码:

/* Groups/add.ctp and Groups/edit.ctp */

<?php echo $this->Form->create('Group');?>
<fieldset>
    <legend><?php echo __('Add Group'); ?></legend>
<?php
    echo $this->Form->input('name');
?>
</fieldset>
<h2>Users</h2>
<table id="mytable">
<tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th></tr>
<tr id="user0" style="display:none;">
    <td><?php echo $this->Form->button('&nbsp;-&nbsp;',array('type'=>'button','title'=>'Click Here to remove this user')); ?></td>
    <td><?php echo $this->Form->input('unused.lastName',array('label'=>'','type'=>'text')); ?></td>
    <td><?php echo $this->Form->input('unused.firstName',array('label'=>'','type'=>'text')); ?></td>
    <td><?php echo $this->Form->input('unused.email',array('label'=>'','type'=>'text')); ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another user','onclick'=>'addUser()')); ?> </td><td></td><td></td><td></td></tr>
</table>
<?php echo $this->Form->end(__('Submit'));?>

/* Javascript */

<script type='text/javascript'>
var lastRow=0;

function addUser() {
    lastRow++;
    $("#mytable tbody>tr:#user0").clone(true).attr('id','user'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr:#trAdd");
    $("#user"+lastRow+" button").attr('onclick','removeUser('+lastRow+')');
    $("#user"+lastRow+" input:first").attr('name','data[User]['+lastRow+'][lastName]').attr('id','userLastName'+lastRow);
    $("#user"+lastRow+" input:eq(1)").attr('name','data[User]['+lastRow+'][firstName]').attr('id','userFirstName'+lastRow);
    $("#user"+lastRow+" input:eq(2)").attr('name','data[User]['+lastRow+'][email]').attr('id','userEmail'+lastRow);
}

function removeUser(x) {
    $("#user"+x).remove();
}

1 个答案:

答案 0 :(得分:0)

如果您只想要编辑表单中的字段,可以使用

检索值
$this->Form->value(Model.fieldname);