我在我看來有以下代碼。我使用選擇下拉菜單來顯示可用選項,所選選項將添加到其下方的列表中。如何將元素添加到視圖中的數組並將其傳遞給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>
您不能運行代碼片段中的PHP(或任何其他服務器端代碼),每個框架上的「HTML」,「CSS」和「JavaScript」標籤都應該顯而易見。編輯。 – Sparky
@Sparky很顯然,我意識到這一點。我只是想展示我的觀點。 – raptorandy
http://api.jquery.com/jquery.ajax/ –