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 :)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报