简体   繁体   English

动态将值绑定到jQuery multiselct控件显示单选按钮

[英]Binding values dynamically to jQuery multiselct control shows radio buttons

I am getting response form Api and I am dynamically creating SELECT control with options. 我正在获取来自Api的响应,并且正在动态创建带有选项的SELECT控件。

This is my HTML 这是我的HTML

         <select id="resourceviewstop"></select>

I am looping response in Javascript as below 我在Javascript中循环响应,如下所示

         $.each(response, function (index, item) {
             $('<option value="' + item.Id+ '">' + item.Description + '</option>').appendTo('#resourceviewstop');
          });

Normal drop down will be created as shown below. 正常下拉列表将如下所示创建。

在此处输入图片说明

Now I am trying to apply multiselect to generated drop down options using jQuery multiselect plugin 现在,我正在尝试使用jQuery multiselect插件将multiselect应用于生成的下拉选项

        $('#resourceviewstop').multiselect({
                 includeSelectAllOption: true
        });

在此处输入图片说明

But I see radio buttons instead of check box. 但是我看到单选按钮而不是复选框。 How can i get checkbox here? 我如何在这里获得复选框?

EDITED EDITED

If i hard code OPTIONS then it will come as checkbox :( But I am getting data from Json and can not hard code. 如果我对选项进行硬编码,那么它将作为复选框出现:(但是我正在从Json获取数据,因此无法进行硬编码。

HTML generated, which i grabbed from developer tool 生成的HTML,我从开发人员工具中获取

在此处输入图片说明

Multiselect has a special way to add new option items.You have to add multiselect('refresh') to render your new options: Multiselect有一种添加新选项的特殊方法。您必须添加 multiselect('refresh')来呈现新选项:

Here is how to add a value: ( http://jsfiddle.net/csdtesting/mpy72gyf/ ) 以下是添加值的方法:( http://jsfiddle.net/csdtesting/mpy72gyf/

var el  = $('#resourceviewstop').multiselect({
         header:false,
         includeSelectAllOption: true
    });
var newItem = $('#newItem');
var opt =  opt = $('<option />');
    $("#add").click(function(){
        var v = newItem.val(), opt = $('<option />', {
            value: v,
            text: v
        });
        opt.appendTo(el);
        el.multiselect('refresh');
    });

So, you have to change the append operation to: 因此,您必须将追加操作更改为:

  $.each(response, function (index, item) {
    var opt = $('<option />', {
                value: item.Id,
                text: item.Description 
            });
            opt.appendTo(el);
            el.multiselect('refresh');
          });

Hope this helps! 希望这可以帮助!

Revisioned solution asked vol.1: http://jsfiddle.net/csdtesting/mpy72gyf/1/ 修订后的解决方案要求第1卷: http//jsfiddle.net/csdtesting/mpy72gyf/1/

vol2 http://jsfiddle.net/mpy72gyf/2/ vol2 http://jsfiddle.net/mpy72gyf/2/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM