最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Deselect top option in <select> - bootstrap multiselect - Stack Overflow

programmeradmin0浏览0评论

I have a bootstrap multiselect html element that i fill with data from a web service call that gets a list of regions in New Zealand. I need to load those options into the <select> and not select the top option by default.

I have tried everything that is done via the bootstrap multiselect built in functions and options. Nothing will work. So i am resorting to vanilla javascript or vanilla jquery to go straight into the plain html and deselect the first option.

All work on this can be done in this jsfiddle

I had to copy paste a minified external resource (xml2json.min.js) into the top of the javascript editor because it wasn't playing nicely when I tried to add it as an external resource.

I have tried this:

$("ul.multiselect-container").find("li").removeClass("active");

And it doesn't work. It removes the active class but doesn't deselect the option. How do I deselect the option?

I have tried this:

$("ul.multiselect-container").find('input[type="radio":checked]').prop('checked', false);

It doesn't deselect the option.

Please see if you can deselect the top option of the select.

So the jsfiddle at url same as above but ending in 182 is my refactoring to try and make it easier for people to understand but it didn't end up working correctly on my friends laptop.

When using the plugin as a <select multiple=true> it doesn't select the top option by default. However I need that same behaviour with a normal <select>

I have a bootstrap multiselect html element that i fill with data from a web service call that gets a list of regions in New Zealand. I need to load those options into the <select> and not select the top option by default.

I have tried everything that is done via the bootstrap multiselect built in functions and options. Nothing will work. So i am resorting to vanilla javascript or vanilla jquery to go straight into the plain html and deselect the first option.

All work on this can be done in this jsfiddle

I had to copy paste a minified external resource (xml2json.min.js) into the top of the javascript editor because it wasn't playing nicely when I tried to add it as an external resource.

I have tried this:

$("ul.multiselect-container").find("li").removeClass("active");

And it doesn't work. It removes the active class but doesn't deselect the option. How do I deselect the option?

I have tried this:

$("ul.multiselect-container").find('input[type="radio":checked]').prop('checked', false);

It doesn't deselect the option.

Please see if you can deselect the top option of the select.

So the jsfiddle at url same as above but ending in 182 is my refactoring to try and make it easier for people to understand but it didn't end up working correctly on my friends laptop.

When using the plugin as a <select multiple=true> it doesn't select the top option by default. However I need that same behaviour with a normal <select>

Share Improve this question edited Mar 23, 2016 at 8:50 BeniaminoBaggins asked Mar 23, 2016 at 6:36 BeniaminoBagginsBeniaminoBaggins 12.6k46 gold badges174 silver badges335 bronze badges 4
  • Do you have codePen or jsFiddle for this one – Sridhar Gudimela Commented Mar 23, 2016 at 6:39
  • @SridharGudimela jsfiddle/bkzb272j/181 thanks – BeniaminoBaggins Commented Mar 23, 2016 at 6:39
  • I tried adding selected='false' in every option at line no 38 in your jsfiddle that actually unselected the first option. However it selected the last one. Will try more and will see if there i any better way to deselect everything or by using selectedIndex = -1. – Sridhar Gudimela Commented Mar 23, 2016 at 7:11
  • The first example here seems to do what you asked for.. davidstutz.github.io/bootstrap-multiselect/#further-examples – Rajshekar Reddy Commented Mar 23, 2016 at 7:34
Add a ment  | 

3 Answers 3

Reset to default 1

I just had a look in the plugin, And here is what I understood, To remove default selection you must have your selection set as multiple='multiple', This is when you can see a Checkbox in the drop down rather than radio buttons.

If you do not put this multiple option set then you will end up in having a radio button and this will set the first value by default. Here is the Fiddle which doesnt not select any default value. I just added the option multiple='multiple' to your dynamic select tag.

To have the option multi select set and also be able to select only one at a time use the below code. You need to replace your code block with this one

        $("body").prepend("<select id=\"a-select\" multiple='multiple' >"             
        + optionsText +
        "</select>");

        $('#a-select').multiselect({
        on: {
            change: function(option, checked) {
                alert('sdd');
                var values = [];
                $('#a-select').each(function() {
                    if ($(this).val() !== option.val()) {
                        values.push($(this).val());
                    }
                });

                $('#a-select').multiselect('deselect', values);
            }
        }
    });

Logic taken from http://davidstutz.github.io/bootstrap-multiselect/#further-examples, see the 5th example from here which says Simulate single selections using checkboxes.

I have redefined the method "deselect" as follows.

 this.deselect = function() {
      $('input[type="radio"]:checked').attr('checked', false);
     $(".multiselect-selected-text").text("None selected");
  }

JsFiddle Demo

Add an empty option to the top of the select element.

发布评论

评论列表(0)

  1. 暂无评论