簡體   English   中英

yii2在表單字段中添加額外的值

[英]yii2 adding a extra value in form field

我在yii2項目中工作。 我有一個帶有文本字段的表格。 形式上的一個字段是長度。 目前,我使用文本字段。 我需要的是一個值文本字段,然后是一個下拉框,我們可以選擇厘米,英寸或像素。 如何做到這一點以及如何在控制器中獲得價值。

您可以通過2種方式來做到這一點:

第一,

使用html helper類獲取一個輸入字段和一個dropDownList,或者使用html創建簡單的dropDownList

<?= $form->field($model, 'length')->textInput(['maxlength' => 255]); ?>
<?= Html::dropDownList('length_type', null,[ 'cm' => 'cm', 'inch' => 'inch', 'pixel' => 'pixel'],['class' => 'form-control','id'=>'length_type']) ?>

如果您想使用html helper類而不是如下導入Html類

use yii\helpers\Html;

現在,在控制器中使用長度類型

if(isset($_POST['length_type']) && $_POST['length_type'] !=null)
{
  $len_type=$_POST['length_type'];
  // use this variable  according to yuor need
}

第二,

模型類中的decalare varibale length_type

鑒於

<?= $form->field($model, 'length')->textInput(['maxlength' => 255]); ?>
<?= $form->field($model, 'length_type')->dropDownList([ 'cm' => 'cm', 'inch' => 'inch', 'pixel' => 'pixel'], ['class' => 'priority_list']) ?>

在控制器中,您可以將模型變量指令用作

$len=$model->length;
$len=$model->length_type;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM