[英]PHP Multiselect with Chosen jQuery - Returning all values not just selected
[英]Is there a way to post all values in a multiselect list and not just the selected ones?
我有兩個MultiSelect列表(AllProductList和SelectedProductList),AllProductList包含特定類別的所有產品,我使用JQuery從AllProductList添加/克隆選項到SelectedProductList。
我顯然只希望在SelectedProductList中發布值,而不管它們是否被選中。
我只在SelectedProductList周圍包裝了表單標簽,現在需要一些方法在其中發布所有選項值,無論是否選中。
你可以編寫一些javascript來填充一個隱藏的表單元素,其中包含select中的所有值,如下所示,並且在服務器端只需使用explode(“,”,$ _ POST [“allValues”])來獲取所有選項
<script>
var hiddenValues = "";
$(document).ready(function(){
$("#mySelect option").each(function(){
hiddenValues = $(this).val() + ",";
})//end each
$("#myForm").append("<input type='hidden' name='allValues' value='"+hiddenValues+"'>")
})
</script>
顯然,上面依賴於jQuery,你的表單的id為myForm,你的multiselect的id為mySelect :)
編輯:
注意1:這只保存選項的值,而不保存選擇的標簽(也可以采用類似的方法來保存這些標簽)。 記住這一點
注意2:請注意,如果值包含任何逗號,因為這將使您的輸入無效(如果沒有以某種方式轉義,或者如果不使用其他分隔符)
我不能評論,所以我想我必須回答???
波格丹的回答需要+ =
hiddenValues += $(this).val() + ",";
否則你只得到1 ......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.