douquqiang1513 2015-01-24 01:45
浏览 37
已采纳

如何创建yii2多个字段? [关闭]

Have a problem with yii2 How i can handle multiple fields? My view must be like this link http://bootsnipp.com/snippets/featured/multiple-fields Please can someone provide an example

  • 写回答

1条回答 默认 最新

  • doze79040 2015-01-24 02:19
    关注

    I think you should using javascript to create fields and php form (not using yii).

    Example:

    The Html:

    <div class="input">
        <div id="container"></div>
        <span class='add-input' href="#" name="name" onclick='addFields()'>Add</span>
    </div>
    

    The JS function:

    function addFields(){
        var container_parent = document.getElementById('container');
    
        var div = document.createElement("div");
        div.name = "div_element";
        container_parent.appendChild(div);
    
        var new_field = document.createElement("input");
        new_field.name = 'new_name[]';
        div.appendChild(new_field);
    }
    

    Here I set name 'new_name[]' to when receive the post data, we using foreach ($_POST['new_name'] as value) to get all data.

    If create select element:

    function addFields(){
        var container_parent = document.getElementById('container');
    
        var div = document.createElement("div");
        div.name = "div_element";
        container_parent.appendChild(div);
    
        var new_field = document.createElement("select");
        new_field.name = 'new_name[]';
        div.appendChild(new_field);
        new_field.innerHTML = '<?php echo $option_items; ?>';
    }
    

    Here I add options value created by yii form:

    $option_items =  $form->dropDownList($model, 'name', $allItems, array('prompt'=>'')); //get dropdownlist by yii
    //remove data, just hold option value
    $pos = strpos($option_items, "<option");
    $rpos = strrpos($option_items, "</option>");
    $option_items = substr($option_items, 0, $rpos+9);
    $option_items = substr($option_items, $pos, strlen($option_items) - $pos);
    $option_items = str_replace("
    ", "", $option_items);
    

    Or you can add your options value at:

    new_field.innerHTML = '<?php echo $option_items; ?>';
    

    You can modify to it cooler, good luck, sorry if my English is bad :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料