1

我有这个表格(我使用了 crud 生成器和模型生成器):

创建猪

[姓名]

[品种]

[病态] [加病态]

每次单击时,如何使添加疾病创建一个新的文本字段?我已经有一个多值疾病 ID 的表,我只需要访问该表并添加所有疾病,使用一个相同的 ID,这样我就可以将它用作表中的外键。

<?php
/* @var $this PigController */  
/* @var $model Pig */
/* @var $form CActiveForm */
?>
<div class="form">      
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'pig-form',
    'enableAjaxValidation'=>true,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">           
        <?php echo $form->labelEx($model,'name'); ?>                                
        <?php echo $form->dropDownList($model,'name');?>
    <?php echo $form->error($model,'name'); ?>      
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'breed'); ?>
        <?php echo $form->dropDownList($model,'breed', $model-  >getBreedOptions()); ?>
        <?php echo $form->error($model,'breed'); ?>
    </div>

    /*
    ENTER CODE FOR SICKNESS HERE
    */
    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>     
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->
4

1 回答 1

2

我也有同样的场景。我所做的是使用脚本。试试这个。它对我有用。请务必进行必要的更改

  $(function(){        
      $("#add").click(function(){

        if(5 > $(".attr").length) {
            var cycleBlock = '<tr id="item'+i+'">';
            cycleBlock += '<td style="width: 447px;"> <label for="CategoryMstExt_Attribute">Attribute</label> <input type="text" id="CategoryMstExt_0_attributes" name="CategoryMstExt['+i+'][attributes]" class="attrName'+i+'" maxlength="100" size="44"> </td> <td> <label for="CategoryMstExt_Data_Type">Data Type</label> <select id="CategoryMstExt_'+i+'_datatype" name="CategoryMstExt['+i+'][datatype]" class="attr" onchange="javascript:checkSelection(this.value,'+i+')"> <option value="17">Textarea</option> <option value="16">Textbox</option> <option value="18">Listbox</option> </select><img  onclick="deleteElm(item'+i+'.id);" src="<?php echo Yii::app()->request->baseUrl; ?>/assets/81ff99cf/gridview/delete.png" alt=""> </td>';
            cycleBlock += '</tr>';
            var $cycleBlock = $(cycleBlock);
            $('#fields').append($cycleBlock);
            i++;
        } else {

            alert('Maximum attributes limit reached');
        }
    });
});



<?php 

    <img style="margin-left:600px;" id="add" src="<?php echo Yii::app()->request->baseUrl; ?>/media/images/add.png">
                    <table id="fields" >
                        <tr id="item1" >
                            <td style="width: 447px;"> 
                                <?php echo $form->labelEx($model, 'Attribute'); ?>
                                <?php echo $form->textField($model, '[0]attributes', array('size' => 44, 'maxlength' => 100, 'class' => 'attrName0')); ?>
                            </td>
                            <td > 
                                <?php echo $form->labelEx($model, 'Data Type'); ?>                                 
                                <?php echo $form->dropDownList($model, '[0]datatype', Array('17' => 'Textarea', '16' => 'Textbox', '18' => 'Listbox'), array('onChange' => 'javascript:checkSelection(this.value,0)', 'class' => 'attr')); ?>
                                <img  onclick="deleteElm(item1.id);" src="<?php echo Yii::app()->request->baseUrl; ?>/media/images/delete_2.png" alt="">
                            </td>
                        </tr>
                    </table>  
?>
于 2012-11-27T10:24:31.887 回答