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

javascript - HTML multi select how to deselect all if select first item using jquery - Stack Overflow

programmeradmin3浏览0评论

I have a multi select control look like below,

When user select "Any", I want to deselect all other selected values and select only "Any", means, if user select "Any", do not allow to select other values else allow to select other values.

How its possible through jQuery

Thanks...

I have a multi select control look like below,

When user select "Any", I want to deselect all other selected values and select only "Any", means, if user select "Any", do not allow to select other values else allow to select other values.

How its possible through jQuery

Thanks...

Share Improve this question edited Sep 28, 2014 at 20:06 Able asked Sep 28, 2014 at 19:23 AbleAble 3,91212 gold badges61 silver badges88 bronze badges 2
  • a fiddle might be helpful – Nikunj Madhogaria Commented Sep 28, 2014 at 19:31
  • you should probably add your answer among the answers instead of part of the question, it's not prohibited... at the same time @AlexChar provided a very nice answer, nice and clean (and short)... – benomatis Commented Sep 28, 2014 at 20:01
Add a ment  | 

3 Answers 3

Reset to default 4
jQuery("#select").change(function() {
    if (jQuery("#select option:first").is(':selected')) {
        jQuery("#select").val("any");
    }
});

To the form below:

<select multiple id="select">
    <option value="any">Any</option>
    <option value="...">...</option>
    ...
</select>

I think you want something like this:

$("select").on("change", function () {
    var selOption = $("option:selected", this).val();
    if (selOption == "any") {
        $(this).attr("selected", false);
        $(this).val("any");
    }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select2" multiple="multiple">
    <option value="any">Any</option>
    <option value="12">twelve</option>
    <option value="13">thirette</option>
    <option value="14">fourteen</option>
    <option value="15">fifteen</option>
    <option value="16">sixteen</option>
    <option value="17">seventeen</option>
    <option value="18">eighteen</option>
    <option value="19">nineteen</option>
    <option value="20">twenty</option>
</select>

In most cases this should work

$('.my-select').val('any')

Where is ’any' is a value of Any option.

(function() {
  var mSelect;

  mSelect = $('select');

  mSelect.on('click', function(event) {
    var values, needReset;
  
    values = mSelect.val();
    needReset = event.target.value === 'any' ||
       (values.length > 1 && values.indexOf('any') !== -1);
  
    if (needReset) {
      mSelect.val('any');
    }
  });
}());
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple>
  <option value="any">Any</option>
  <option value="divorced">Maried</option>
  <option value="unmarried">Unmarried</option>
</select>

发布评论

评论列表(0)

  1. 暂无评论