2017-03-16 64 views
0

我在我看来有以下代码。我使用选择下拉菜单来显示可用选项,所选选项将添加到其下方的列表中。如何将元素添加到视图中的数组并将其传递给codeigniter中的控制器?

我想保存所选的选项(“礼”元素)数组中的,所以我可以在我的控制器使用此功能,并保存在一个datbase数组值

$this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $nombresFamilia); 

,会是什么达到这个目标的最佳方式

$("#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> 
+0

您不能运行代码片段中的PHP(或任何其他服务器端代码),每个框架上的“HTML”,“CSS”和“JavaScript”标签都应该显而易见。编辑。 – Sparky

+0

@Sparky很显然,我意识到这一点。我只是想展示我的观点。 – raptorandy

+0

http://api.jquery.com/jquery.ajax/ –

回答

0

试试这个:

样本列表

<ul id="selectedList"> 
    <li>test 1</li> 
    <li>test 2</li> 
    <li>test 3</li> 
</ul> 

脚本

var asd = $("#selectedList li"); 
var list_holder_array = []; 

asd.each(function() { 
    list_holder_array.push($(this).text()); 
}); 

产生

list_holder_array = Array [ "test 1", "test 2", "test 3" ] 

现在你必须在数组中的李内容,您现在可以通过AJAX它传递给你的控制器,你可以做任何你需要它

+0

谢谢,它的作品。你能推荐我有关如何使用Ajax来做到这一点的任何资源? – raptorandy

+0

@raptorandy看这里:[here](http://stackoverflow.com/questions/8486132/jquery-ajax-passing-value-from-mvc-view-to-controller)或[here](http:// stackoverflow .com/questions/27743287/how-to-send-ajax-data-from-view-to-controller-phpmvcajax)和jquery [site](http://api.jquery.com/jquery.ajax/) –

相关问题