简体   繁体   English

使用select2插件添加或删除项目时如何更改隐藏的输入值

[英]How can I change hidden input val when add or remove item with using select2 plugin

I use select2 plugin and get values when user typing least 3 character. 我使用select2插件,并在用户键入至少3个字符时获取值。 Get data from php as json like this; 像这样以json格式从php获取数据;

"1":"val1"  ,  "5":"val2"  ,  "19":"val3"....

I want to store id values of selected items at hidden input and when user remove any selected item, the id of removed item also remove from hidden input. 我想在隐藏输入中存储选定项的ID值,并且当用户删除任何选定项时,被删除项的ID也会从隐藏输入中删除。 For example; 例如;

When val1 and val2 items are selected like below, value of hidden input (id which 'hdn-id') change like below, also. 当像下面一样选择val1和val2项目时,隐藏输入的值(“ hdn-id”的id)也会像下面那样变化。

在此处输入图片说明

<input type="hidden" id="hdn-id" val="1,5" />

And when val1 is removed, id of this item (1) removed from hidden input like this ; 并且当val1删除时,此项的ID(1)的ID从这样的隐藏输入中删除;

在此处输入图片说明

<input type="hidden" id="hdn-id" val="5" />

But I can't do this. 但是我做不到。 My codes; 我的密码;

SELECT2: 选择2:

function selectAjax(element,url,hiddenElement) {

var selectedItemsArray = []
$('#'+element).select2({

    multiple: multi,

    id: function(element) {
        return element
    },

    ajax: {
        url: url,
        dataType: 'json',
        data: function(term,page) {
            return {
                term: term,
                page_limit: 10
            };                        
        },
        results: function(data,page) {
            var titleArr    = [];
            $.each(data, function(k,v){
               titleArr.push(k+':'+v);
            });

            return {
                results: titleArr
            };
        }
    },

    formatResult: formatResult,
    formatSelection: formatSelection,

});

function formatResult(data) {
   return '<div>'+data.substr(data.indexOf(':')+1)+'</div>'
};

function formatSelection(data) {

    var id   = data.split(':',1),
        text = data.substr(data.indexOf(':')+1),                    
        hiddenElementValue = eval([jQuery('#'+hiddenElement).val()]);
        selectedItemsArray.push(id);
        jQuery('#'+hiddenElement).val(selectedItemsArray);

    return '<div data-id="'+id+'" class="y-select2-selected-items">'+text+'</div>';        
};


}


selectAjax('select2-element','ajx.php','hdn-id');

HTML: HTML:

<input type="text" id="select2-element" />
<input type="hidden" id="hdn-id" />

I can store ids at hidden input with above code but when remove an item I can't remove id from hidden input. 我可以使用上面的代码将ID存储在隐藏的输入中,但是当删除项目时,我无法从隐藏的输入中删除ID。 Because plugin assign 'return false' to element's onclick event. 因为插件将'return false'分配给元素的onclick事件。 I handed the job with above codes, I think.How can I be a better solution? 我认为我是用上述代码来完成这项工作的。如何才能成为更好的解决方案?

You can use the change event of the select2 plugin and there write some code that will update the value of the hidden input. 您可以使用select2插件的change事件,然后编写一些代码来更新隐藏输入的值。

    $("#select2-item").select2({
        //options go here
    });
    $("#select2-item").on("change", function(e) { 
        //update hidden input value
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用select2时如何删除Materialize的select下拉列表? - How can I remove Materialize's select dropdown when using select2? 如何使用 jQuery Select2 插件删除与当前键入的文本匹配的下拉项? - How to remove the dropdown item matching the currently typed text using the jQuery Select2 plugin? 在将选项文本分配给隐藏输入时,如何从选择选项中删除HTML nbsp标记 - How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input 如何为某些选择下拉列表禁用select2插件? - How can I disable the select2 plugin for certain select dropdowns? 当我使用select2 jquery选择&#39;ALL&#39;项目时,如何禁用选择框中的所有项目 - How to disable all items in select box when i am selecting 'ALL' item using select2 jquery 如何使用带有 codeigniter 的 Select2 插件将多个数据插入到我的数据库中? - How can I insert multiple data to my database using the Select2 plugin with codeigniter? select2:输入更改时启动功能 - select2 : launch function when input change 使用时如何从select2中获取选定的文本<input> - How to get Selected Text from select2 when using <input> 如何使用SELECT2输入框方法设置默认值 - How can I set a default value using SELECT2 input box method 如何更改 Select2 箭头图标? - How can I change Select2 arrow icon?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM