How to add fields to activeform with js/jQuery in Yii2?

前端 未结 1 1629
不思量自难忘°
不思量自难忘° 2021-01-16 09:20

Hi I have a user form and I want to add a button so that the user can add more contacts dynamically.

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 09:56

    You can do it like below:

    Write the following code into your view:

     $this->registerJs('$("#btnadd").on("click",function(){'
        . '$("#dynamicInput").append(\''
        . Html::tag("div",  Html::textInput("name","",['placeholder'=>"name"]))
        . Html::tag("div",  Html::textInput("email","",['placeholder'=>"email"]))
        . '\');'
        . '})');
    

    Please note that you need to have:

    use yii\helpers\Html;
    

    at top of your page. Otherwise, you should write yii\helpers\Html:: instead of Html::

    0 讨论(0)
提交回复
热议问题