1

我在 _form 中有一个链接:

<?php echo CHtml::link('Add Child', '#', array('id' => 'loadChildByAjax')); ?>

renderPartial child _form 的一些代码:

<div id="children">
    <?php
    $index = 0;
    foreach ($model->goodsservices as $id => $child):
        $this->renderPartial('goods/_form', array(
            'model' => $child,
            'index' => $id,
            'display' => 'block'
        ));
        $index++;
    endforeach;
    ?>
</div>

并使用 AJAX 编写脚本:

<?php
Yii::app()->clientScript->registerScript('loadchild', '
var _index = ' . $index . ';
$("#loadChildByAjax").click(function(e){
e.preventDefault();
var _url = "' . Yii::app()->controller->createUrl("loadChildByAjax", array("load_for" => $this->action->id)) . '&index="+_index;
$.ajax({
    url: _url,
    success:function(response){
        $("#children").append(response);
        $("#children .crow").last().animate({
            opacity : 1, 
            left: "+50", 
            height: "toggle"
        });
    }
});
_index++;
});
', CClientScript::POS_END);
?>

在我的控制器中:

public function actionLoadChildByAjax($index)
{
    $model = new AppGoodsServices;
    $this->renderPartial('goods/_form', array(
        'model' => $model,
        'index' => $index,
    ), false, true);
}

最后是我的孩子_form:

<div style="margin-bottom: 20px; display: <?php echo!empty($display) ? $display : 'none'; ?>; width:100%; clear:left;" class="crow">
<div class="row" style="float: left;">
    <?php echo CHtml::activeLabelEx($model, '['.$index.']goods_and_services'); ?>
    <?php echo CHtml::activeTextField($model, '['.$index.']goods_and_services',     array('size' => 30, 'maxlength' => 150)); ?>
    <?php echo CHtml::error($model, '['.$index .']goods_and_services'); ?>
</div>

<div class="row" style="float: left;">
    <?php echo CHtml::activeLabelEx($model, '['.$index .']percentage'); ?>
    <?php echo CHtml::activeTextField($model, '['.$index.']percentage', array('size' => 5)); ?>
    <?php echo CHtml::error($model, '['.$index.']percentage'); ?>
</div>
<div style="clear: both;"></div>
<div class="row">
    <?php echo CHtml::link('Delete', '#', array('onclick' => 'deleteChild(this,     '.$index.'); return false;'));
    ?>
</div>
</div>
<div style="clear: both;"></div>
<?php
Yii::app()->clientScript->registerScript('deleteChild', "
function deleteChild(elm, index)
{
element=$(elm).parent().parent();
/* animate div */
$(element).animate(
{
    opacity: 0.25, 
    left: '+=50', 
    height: 'toggle'
}, 500,
function() {
    /* remove div */
    $(element).remove();
});
}", CClientScript::POS_END);
?>

我需要当我的按钮被单击时,应该动态呈现子 _form,但 id 不起作用。我在这里想念什么?

4

1 回答 1

0

而是使用 yii 的小部件

<?php echo CHtml::activeLabelEx($model, '['.$index.']goods_and_services'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']goods_and_services', array('size' => 30, 'maxlength' => 150)); ?>
<?php echo CHtml::error($model, '['.$index .']goods_and_services'); ?>

仅使用 html,例如

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

        if(5 > $(".attr").length) {
            var cycleBlock = '<input type="text" id="CategoryMstExt_0_attributes" name="CategoryMstExt['+i+'][attributes]" class="attrName'+i+'" maxlength="100" size="44"> ';
            var $cycleBlock = $(cycleBlock);
            $('#fields').append($cycleBlock);
            i++;
        } else {
            alert('Maximum attributes limit reached');
        }
    });
});
于 2015-07-21T16:24:07.017 回答