douxin8749
2015-11-16 10:44
浏览 226
已采纳

yii2如何获取textInput值并发送到另一个textInput

I am new for Yii2 and I have a question.

In my _form.php (generated automatically by gii) I have two fields (textInput)

<?= $form->field($model, 'Test1')->textInput() ?>
<?= $form->field($model, 'Test2')->textInput() ?>

I want to get Test1's (textInput) value and get Test2's value, and send the total value to another textInput.

<?= $form->field($model, 'Test3')->textInput() ?>

I want to see the total value in the Test3 (textInput).

Can anyone help me please..

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongna2498 2015-11-16 11:37
    已采纳

    This can be done by adding custom javascript in your view

    <?php
    $this->registerJs('
    
        jQuery(document).on("change" ,"#'. Html::getInputId($model ,'Test2') .'" ,function(){
            $("#'. Html::getInputId($model ,'Test3') .'").val();
            var first = $("#'. Html::getInputId($model ,'Test1') .'").val();
            var second = $("#'. Html::getInputId($model ,'Test2') .'").val();
            var third = first +"  " + second;
            $("#'. Html::getInputId($model ,'Test3') .'").val(third);
    
        });
    
    ');
    
    ?>
    
    打赏 评论
  • dongrong1856 2015-11-16 11:30
    1. Add Test3 to your model as property.
    2. Create getter

    public function getTest3() { return $this->Test1 . " " . $this->Test2; }

    打赏 评论

相关推荐 更多相似问题