如何限制在html选择框中选择的选项?

tom*_*ted 25 html javascript forms jquery

我正在使用我正在制作的形式中的select标签允许多个选择,但我想将最大选择量提高到10.这可能使用javascript或jquery吗?

提前致谢!

Top*_*gio 29

这里有一些完整的代码供您使用...得热爱Google AJAX API游乐场:-)

编辑1:注意:这只能让你选择5因为我不想复制/粘贴另外10个选项:-)

<!--
  copyright (c) 2009 Google inc.

  You are free to copy and use this sample.
  License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Sample Select Maximum with jQuery</title>
    <script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q">    </script>
    <script type="text/javascript">
    google.load("jquery", "1");

    $(document).ready(function() {

      var last_valid_selection = null;

      $('#testbox').change(function(event) {
        if ($(this).val().length > 5) {
          alert('You can only choose 5!');
          $(this).val(last_valid_selection);
        } else {
          last_valid_selection = $(this).val();
        }
      });
    });
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <select multiple id='testbox'>
      <option value='1'>First Option</option>
      <option value='2'>Second Option</option>
      <option value='3'>Third Option</option>
      <option value='4'>Fourth Option</option>
      <option value='5'>Fifth Option</option>
      <option value='6'>Sixth Option</option>
      <option value='7'>Seventh Option</option>
      <option value='8'>Eighth Option</option>
      <option value='9'>Ninth Option</option>
      <option value='10'>Tenth Option</option>
    </select>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

演示

  • @Topher,除非你准备好读心思,否则不要回答!:) (2认同)

Sam*_*son 16

这会将用户限制为3个选项:

$("select").on("click", "option", function () {
    if ( 3 <= $(this).siblings(":selected").length ) {
        $(this).removeAttr("selected");
    }
});??????????
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/2GrYk/

  • 如果我们使用键盘或滑动点击,这将无效. (7认同)

Ori*_*iol 5

这个答案适用于这些情况:

  • 正常点击
  • 只有mousedown(鼠标在鼠标前移动到外面)
  • 键盘
  • 如果有不同的选项具有相同的值

码:

$('#mySelect').on('change', function() {
    if (this.selectedOptions.length < 4) {
        $(this).find(':selected').addClass('selected');
        $(this).find(':not(:selected)').removeClass('selected');
    }else
        $(this)
        .find(':selected:not(.selected)')
        .prop('selected', false);
});
Run Code Online (Sandbox Code Playgroud)

演示

或者,对于不支持的旧浏览器selectedOptions,

$('#mySelect').on('change', function() {
    var $sel = $(this).find(':selected');
    if ($sel.length < 4) {
        $sel.addClass('selected');
        $(this).find(':not(:selected)').removeClass('selected');
    }else
        $(this)
        .find(':selected:not(.selected)')
        .prop('selected', false);
});
Run Code Online (Sandbox Code Playgroud)

演示