lkle 2012-06-09 17:45:10 7646次浏览 3条回复 0 0 0

yii怎么动态的增加或删除表单元素,就是用ajax动态的增加或删除一个文本框,或单选按钮什么的。。。

  • 回复于 2012-06-09 18:44 举报

    直接jquery绑定方法 before() 添加 不就好了?

  • 回复于 2012-06-09 19:04 举报

    我上次做的添加多条工作记录,三段代码放在三个文件里。

    效果:
    [attach]756[/attach][code]

    <table>
        <tr>
            <th><?php echo WorkExperience::model()->getAttributeLabel('start_date')?></th>
        	<th><?php echo WorkExperience::model()->getAttributeLabel('end_date')?></th>
            <th><?php echo WorkExperience::model()->getAttributeLabel('organization_name')?></th>
            <th><?php echo WorkExperience::model()->getAttributeLabel('job_title')?></th>
            <th><?php echo $batchStudents->status?"":CHtml::link(t('Add'), 'javascript:void(0);', array('onClick'=>'addWorkExperience($(this))', 'class'=>'add'));?></th>
        </tr>
        <?php foreach($workExperienceManager->items as $id=>$workExperience){
            $this->renderPartial('workExperienceform', array('id'=>$id,'model'=>$workExperience, 'form'=>$form));
        } ?>
    </table>
    <?php $this->renderPartial('workExperienceJs', array('workExperiences'=>$workExperienceManager, 'form'=>$form));?>
    
    <script type="text/javascript">
    var lastWorkExperience=<?php echo $workExperiences->lastNew?>;
    var trWorkExperience=new String(<?php echo CJSON::encode($this->renderPartial('workExperienceform', array('id'=>'idRep', 'model'=>new WorkExperience, 'form'=>$form), true));?>);
     
    function addWorkExperience(button)
    {
        lastWorkExperience++;
        button.parents('table').children('tbody').append(trWorkExperience.replace(/idRep/g,'n'+lastWorkExperience));
    }
    function deleteWorkExperience(button)
    {
        button.parents('tr').detach();
    }
    </script>
    
    <tr>
        <td>
              <?php 
              echo $form->textField($model,"[$id]start_date",array('style'=>'width:100px','class'=>'hashDatepicker',
                  'onClick'=>"$('.hashDatepicker').datepicker({
                            dateFormat: 'yy-mm-dd',     
                            changeYear: true
                    });")); ?>        <?php echo $form->error($model,"start_date"); ?>
            
        </td>
       <td>
              <?php echo $form->textField($model,"[$id]end_date",array('style'=>'width:100px','class'=>'hashDatepicker'  ,
                  'onclick'=>"$('.hashDatepicker').datepicker({
                            dateFormat: 'yy-mm-dd',
                            changeYear: true
                    });")); ?>
            <?php echo $form->error($model,"[$id]end_date"); ?>
        </td>
        <td>
              <?php echo $form->textField($model,"[$id]organization_name",array('size'=>20)); ?>
            <?php echo $form->error($model,"organization_name"); ?>
        </td>
         <td>
              <?php echo $form->textField($model,"[$id]job_title",array('size'=>20)); ?>
              <?php echo $form->error($model,"job_title"); ?>
           
        </td>
        <td> <?php echo CHtml::link(
                    t('Delete'), 
                    'javascript:void(0);', 
                    array(
                        'class'=>'delete',
                        'onClick'=>'deleteWorkExperience($(this))',
                        ));?>
        </td>
    </tr>
    
  • 回复于 2012-06-13 15:15 举报

    楼上好东西

您需要登录后才可以回复。登录 | 立即注册