简体   繁体   English

如何在视图中向数组添加元素并将其传递给codeigniter中的控制器?

[英]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.

相关问题 将数据数组从视图传递到Controller Codeigniter - Pass data array from view to Controller codeigniter CodeIgniter - 如何将值从视图传递到控制器? - CodeIgniter - How to pass a value from a view to a controller? CodeIgniter:将Javascript数组从View传递到Controller,然后存储到数据库 - CodeIgniter: Pass Javascript Array from View to Controller then store to Database 如何在Codeigniter中使用Ajax将变量从Controller传递到View? - How to pass variable from Controller to View with Ajax in Codeigniter? 如何通过CodeIgniter上的json从控制器传递数据以进行查看? - How to pass data from controller to view via json on CodeIgniter? 如何从控制器发送json数组以在CodeIgniter中查看 - how to send json array from controller to view in CodeIgniter 如何从Codeigniter视图将数组传递给jquery脚本 - How to pass an array to a jquery script from a codeigniter view 使用AJAX将javascript数组传递给CodeIgniter控制器 - Using AJAX to pass javascript array to CodeIgniter controller 我想使用ajax post从视图传递codeigniter中的数据,在控制器中处理该数据,然后将结果数组发送回视图 - I want to pass data in codeigniter from view using ajax post, process that data in controller and send back the result array to view 如何在Spring MVC中将数组变量从视图传递到控制器 - How to pass array variable from view to controller in spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM