我需要通过ajax加载表单的一部分,但是yii2本身添加了标签表单。应该创建没有标签的HTML格式
<?php $form = new \yii\widgets\ActiveForm(); ?>
<div id="cropped_image">
<img class="image" height="<?=$size->getHeight();?>" width="<?=$size->getWidth();?>" src="<?=$imageModel->getImageUrl()?>"/>
<div class="inline-labels">
<?= $form->field($cropModel, 'x1')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'y1')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'x2')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'y2')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'w')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'h')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'image')->hiddenInput()->label(false); ?>
</div>
</div>
结果是
<form id="w0" action="/admin/image/create" method="post"> <input type="hidden" name="_csrf" value="M0NMbk9xaEhnMx5eOEArG3EuegQoPV0yXG58XX4EAwBsMSo0KTsEKQ=="> <div id="cropped_image"> <img class="image" height="631" width="946" src="/upload/image/cec8e8f39a3cdd43.jpg"/> <div class="inline-labels"> <div class="form-group field-cropform-x1 required"> <input type="hidden" id="cropform-x1" class="form-control" name="CropForm[x1]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-y1 required"> <input type="hidden" id="cropform-y1" class="form-control" name="CropForm[y1]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-x2 required"> <input type="hidden" id="cropform-x2" class="form-control" name="CropForm[x2]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-y2 required"> <input type="hidden" id="cropform-y2" class="form-control" name="CropForm[y2]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-w required"> <input type="hidden" id="cropform-w" class="form-control" name="CropForm[w]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-h required"> <input type="hidden" id="cropform-h" class="form-control" name="CropForm[h]"> <div class="help-block"></div> </div> <div class="form-group field-cropform-image required"> <input type="hidden" id="cropform-image" class="form-control" name="CropForm[image]" value="cec8e8f39a3cdd43"> <div class="help-block"></div> </div> </div> </div>
如果我不是通过ajax添加这部分 - 一切正常,只有一个标签。
答案 0 :(得分:0)
如果您不想要表格标签:
<div id="cropped_image">
<img class="image" height="<?=$size->getHeight();?>" width="<?=$size->getWidth();?>" src="<?=$imageModel->getImageUrl()?>"/>
<div class="inline-labels">
<?= Html::activeHiddenInput($cropModel, 'x1') ?>
<?= Html::activeHiddenInput($cropModel, 'y1') ?>
<?= Html::activeHiddenInput($cropModel, 'x2') ?>
<?= Html::activeHiddenInput($cropModel, 'y2') ?>
<?= Html::activeHiddenInput($cropModel, 'w') ?>
<?= Html::activeHiddenInput($cropModel, 'h') ?>
<?= Html::activeHiddenInput($cropModel, 'image') ?>
</div>
</div>
在您的代码中,标签不会出现。正确的方法:
<?php $form = \yii\widgets\ActiveForm::begin(); ?>
<div id="cropped_image">
<img class="image" height="<?=$size->getHeight();?>" width="<?=$size->getWidth();?>" src="<?=$imageModel->getImageUrl()?>"/>
<div class="inline-labels">
<?= $form->field($cropModel, 'x1')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'y1')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'x2')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'y2')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'w')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'h')->hiddenInput()->label(false); ?>
<?= $form->field($cropModel, 'image')->hiddenInput()->label(false); ?>
</div>
</div>
<?= \yii\widgets\ActiveForm::end(); ?>