[英]How to add elements to an array in a view and pass it to a controller in codeigniter?
I have the following code in my view. 我认为以下代码是正确的。 I use a select dropdown to show the available options, the selected options get added to a list below it.
我使用选择下拉菜单显示可用选项,所选选项将添加到其下方的列表中。
I'd like to save the selected options ("li" elements) in an array so I can use this function in my controller and save the array values in a datbase 我想将选定的选项(“ li”元素)保存在数组中,以便可以在控制器中使用此功能,并将数组值保存在datbase中
$this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $nombresFamilia);
What would be the best way to achieve this? 实现这一目标的最佳方法是什么?
$("#agregarFamilia").click(function() {
if ($('#idFamilia').val() != 0) {
var names = $('#idFamilia').find('option:selected').text();
$("#idFamilia option:selected").remove();
$('#selectedList').append('<li>' + names + '<button type="button" class="delete btn btn-danger btn-xs pull-right">Quitar</button></li>')
}
});
$("body").on("click", ".delete", function() {
var name = $(this).parent().text().replace(/Quitar/, '');
$(this).parent().remove();
$("#idFamilia").append($("<option></option>").val(name).html(name));
//Returns the removed item to the dropdown list in alphabetical position
var foption = $('#idFamilia option:first');
var soptions = $('#idFamilia option:not(:first)').sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
});
$('#idFamilia').html(soptions).prepend(foption);
});
#selectedList li {
margin-bottom: 10px
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="form-group">
<div class="col-md-6">
<select id="idFamilia" name="idFamilia" class="form-control">
<option value="0">Select</option>
<option value="PCR">PCR</option>
<option value="CABLES">CABLES</option>
</select>
</div>
<div class="col-md-2">
<a style="display:block;width:145px" id="agregarFamilia" class="btn btn-primary">
<span class="fa fa-plus"></span> Add
</a>
</div>
</div>
<div>
<ul id="selectedList"></ul>
</div>
try this one: 试试这个:
sample list 样品清单
<ul id="selectedList">
<li>test 1</li>
<li>test 2</li>
<li>test 3</li>
</ul>
script 脚本
var asd = $("#selectedList li");
var list_holder_array = [];
asd.each(function() {
list_holder_array.push($(this).text());
});
produces 产生
list_holder_array = Array [ "test 1", "test 2", "test 3" ]
now you have the li contents in array, you can now pass this to your controller via ajax then you can do whatever you need it for 现在您将li内容放入数组中,现在可以通过ajax将其传递给控制器,然后您就可以执行所需的任何操作了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.