2017-03-16 68 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/) –

相關問題