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 :)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号