函数添加新输入


function add new input

我有add_person.ctp文件,那里有输入和函数,但是这个函数不起作用,我的问题是为什么它不起作用,我没有看到这段代码有任何错误。如果我单击按钮,那么这什么也没做。

这是我的代码

<h2>People</h2>
    <table id="mytable">
    <tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th><th>Gender</th></tr>
    <tr id="person0" style="display:none;">
        <td><?php echo $this->Form->button('&nbsp;-&nbsp;',array('type'=>'button','title'=>'Click Here to remove this person')); ?></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>
        <td><?php echo $this->Form->input('unused.gender',array('label'=>'','type'=>'select','options'=>array('M'=>'M','F'=>'F','T'=>'T'))); ?></td>
    </tr>
    <tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another person','onclick'=>'addPerson()')); ?> </td><td></td><td></td><td></td><td></td></tr>
    </table>
    <?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
<script type='text/javascript'>
    var lastRow=0;
    function addPerson() {
        lastRow++;
        $("#mytable tbody>tr:#person0").clone(true).attr('id','person'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr:#trAdd");
        $("#person"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
        $("#person"+lastRow+" input:first").attr('name','data[Person]['+lastRow+'][lastName]').attr('id','personLastName'+lastRow);
        $("#person"+lastRow+" input:eq(1)").attr('name','data[Person]['+lastRow+'][firstName]').attr('id','personFirstName'+lastRow);
        $("#person"+lastRow+" input:eq(2)").attr('name','data[Person]['+lastRow+'][email]').attr('id','personEmail'+lastRow);
        $("#person"+lastRow+" select").attr('name','data[Person]['+lastRow+'][gender]').attr('id','personGender'+lastRow);
    }
    function removePerson(x) {
        $("#person"+x).remove();
    }
</script>

感谢您的帮助!

你在这里有错误的选择器#mytable tbody>tr:#person0这里和这里#mytable tbody>tr:#trAdd.分别替换为#mytable tbody>tr#person0#mytable tbody>tr#trAdd。请记住,无需在选择器中使用冒号符号。