2016-06-06 8 views
0

Ich bin ein Formular mit yii2 machen, ich habe jetzt zwei Bereiche:Nehmen Sie Daten von einem anderen Feld in yii2 Form

<?php echo $form->field($model, 'Protocol')->textInput(['maxlength' => true])->dropDownList(
        array("rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"),  // Flat array ('id'=>'label') 
        ['prompt'=>'Select']  // options 
       ); ?> 

<?php echo $form->field($model, 'url')->textInput(['maxlength' => true]); ?> 

Sie sehen wie folgt aus: form

Wie kann ich die Auswahl aus Protokoll-Dropdown-Liste und fügt sie automatisch zum folgenden URL-Feld hinzu? Wie folgt: enter image description here Ich tippe die http:// in das Feld manuell, gibt es trotzdem kann ich es automatisch machen?

Antwort

2

Fügen Sie onchange Ereignis in Ihre 'protocol' DropDownList hinzu. zeige unten Code

<?= $form->field($model, 'Protocol')->dropdownList(["rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"], [ 
    'onchange'=>'$("#'.Html::getInputId($model, 'url').'").val($(this).val());' 

    ]) ?> 

<?= $form->field($model, 'url')->textInput(['maxlength' => true]); 
?> 
+0

Es hat funktioniert, vielen Dank. – ProudNoob

+0

Kann ich diesen Code für das normale Textfeld verwenden? TextInput (['maxlength' => true]) '? – ProudNoob

+1

@ProudNoob. Ja, Sie können nur ID oder Name von TextInput(). –

Verwandte Themen