【问题标题】:bootstrap multiselect get selected values引导多选获取选定的值
【发布时间】:2013-12-29 06:00:54
【问题描述】:

如何在我的多选下拉列表中重新启用 onChange 所有选定值。使用插件here。尝试使用以下内容,但我认为我走错了路

$('#multiselect1').multiselect({
        selectAllValue: 'multiselect-all',
        enableCaseInsensitiveFiltering: true,
        enableFiltering: true,
        maxHeight: '300',
        buttonWidth: '235',
        onChange: function(element, checked) {
            var brands = $('#multiselect1 option:selected');
            var selection = [];
            $(brands).each(function(index, brand){
                selection.push(brand);
            });

            console.log(selection);
        }
    });

找到了

$('#multiselect1').multiselect({
    selectAllValue: 'multiselect-all',
    enableCaseInsensitiveFiltering: true,
    enableFiltering: true,
    maxHeight: '300',
    buttonWidth: '235',
    onChange: function(element, checked) {
        var brands = $('#multiselect1 option:selected');
        var selected = [];
        $(brands).each(function(index, brand){
            selected.push([$(this).val()]);
        });

        console.log(selected);
    }
});

【问题讨论】:

    标签: jquery twitter-bootstrap multi-select


    【解决方案1】:

    我发现在我的案例中有效的解决方案

    $('#multiselect1').multiselect({
        selectAllValue: 'multiselect-all',
        enableCaseInsensitiveFiltering: true,
        enableFiltering: true,
        maxHeight: '300',
        buttonWidth: '235',
        onChange: function(element, checked) {
            var brands = $('#multiselect1 option:selected');
            var selected = [];
            $(brands).each(function(index, brand){
                selected.push([$(this).val()]);
            });
    
            console.log(selected);
        }
    }); 
    

    【讨论】:

    • +1 以获得解决方案,但是您的 selected.push 语句中有错误。您正在推送选项值的列表,而不是作为字符串的值。你想删除括号。
    【解决方案2】:

    短版:

    $('#multiselect1').multiselect({
        ...
        onChange: function() {
            console.log($('#multiselect1').val());
        }
    }); 
    

    【讨论】:

    • 太棒了!非常感谢。
    • 为我在 IE11 中工作
    【解决方案3】:

    效率更高,因为 DOM 查找更少:

    $('#multiselect1').multiselect({
        // ...
        onChange: function() {
            var selected = this.$select.val();
            // ...
        }
    });
    

    【讨论】:

    • 这是最好的答案。请注意,它确实返回所有检查值的数组,因为其他一些答案正在尝试手动执行。未选择时返回 Null。
    【解决方案4】:
    $('#multiselect1').on('change', function(){
        var selected = $(this).find("option:selected");
        var arrSelected = [];
        selected.each(function(){
           arrSelected.push($(this).val());
        });
    });
    

    【讨论】:

    • 确实有帮助
    【解决方案5】:

    在我的例子中,我使用 jQuery 验证规则

    submitHandler 函数并以new FormData(form); 提交数据

    选择选项

    <select class="selectpicker" id="branch" name="branch" multiple="multiple" title="Of Branch" data-size="6" required>
      <option disabled>Multiple Branch Select</option>
      <option value="Computer">Computer</option>
      <option value="Civil">Civil</option>
      <option value="EXTC">EXTC</option>
      <option value="ETRX">ETRX</option>
      <option value="Mechinical">Mechinical</option>
    </select>
    
    <input type="hidden" name="new_branch" id="new_branch">
    

    获取多选值并设置为new_branch输入值&提交表单时从new_branch获取值

    只需将hidden 替换为text 即可查看输出

    <input type="text" name="new_branch" id="new_branch">
    

    脚本(jQuery / Ajax)

    <script>
        $(document).ready(function() {
    
          $('#branch').on('change', function(){
              var selected = $(this).find("option:selected"); //get current selected value
              var arrSelected = []; //Array to store your multiple value in stack
              selected.each(function(){
                arrSelected.push($(this).val()); //Stack the value
              });
              $('#new_branch').val(arrSelected); //It will set the multiple selected value to input new_branch
          });
    
        });
    </script>
    

    【讨论】:

      【解决方案6】:

      请在您的 Html 页面中添加

      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <title>Test the multiselect with ajax</title>
      
          <!-- Bootstrap -->
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
          <!-- Bootstrap multiselect -->
          <link rel="stylesheet" href="http://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css">
      
          <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
          <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
          <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
          <![endif]-->
        </head>
        <body>
          <div class="container">
            <br>
      
            <form method="post" id="myForm">
      
              <!-- Build your select: -->
              <select name="categories[]" id="example-getting-started" multiple="multiple" class="col-md-12">
                <option value="A">Cheese</option>
                <option value="B">Tomatoes</option>
                <option value="C">Mozzarella</option>
                <option value="D">Mushrooms</option>
                <option value="E">Pepperoni</option>
                <option value="F">Onions</option>
                <option value="G">10</option>
                <option value="H">11</option>
                <option value="I">12</option>
              </select>
              <br><br>
              <button type="button" class="btnSubmit"> Send </button>
      
            </form>
      
            <br><br>
            <div id="result">result</div>
          </div><!--container-->
      
          <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
          <!-- Include all compiled plugins (below), or include individual files as needed -->
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
          <!-- Bootstrap multiselect -->
          <script src="http://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
      
          <!-- Bootstrap multiselect -->
          <script src="ajax.js"></script>
      
          <!-- Initialize the plugin: -->
          <script type="text/javascript">
            $(document).ready(function() {
      
              $('#example-getting-started').multiselect();
      
            });
          </script>
      
        </body>
      </html>
      

      请在您的 ajax.js 页面中添加

      $(document).ready(function () {
        $(".btnSubmit").on('click',(function(event) {
          var formData = new FormData($('#myForm')[0]);
          $.ajax({
            url: "action.php",
            type: "POST",
            data: formData,
            contentType: false,
            cache: false,
            processData:false,
            success: function(data)
            {
              $("#result").html(data);
      
              // To clear the selected options
              var select = $("#example-getting-started");
              select.children().remove();
              if (data.d) {
                $(data.d).each(function(key,value) {
                  $("#example-getting-started").append($("<option></option>").val(value.State_id).html(value.State_name));
                });
              }
              $('#example-getting-started').multiselect({includeSelectAllOption: true});
              $("#example-getting-started").multiselect('refresh');
      
            },
            error: function()
            {
              console.log("failed to send the data");
            }
          });
        }));
      });
      

      在你的action.php页面中添加

        echo "<b>You selected :</b>";
      
        for($i=0;$i<=count($_POST['categories']);$i++){
      
          echo $_POST['categories'][$i]."<br>";
      
        }
      

      【讨论】:

        【解决方案7】:
        $('#multiselect1').on('change', function(){
            var selected = $(this).find("option:selected");    
            var arrSelected = [];
        
            // selected.each(function(){
            //   arrSelected.push($(this).val());
            // });
        
            // The problem with the above selected.each statement is that  
            // there is no iteration value.
            // $(this).val() is all selected items, not an iterative item value.
            // With each iteration the selected items will be appended to 
            // arrSelected like so    
            //
            // arrSelected [0]['item0','item1','item2']
            // arrSelected [1]['item0','item1','item2'] 
        
            // You need to get the iteration value. 
            //
            selected.each((idx, val) => {
                arrSelected.push(val.value);
            });
            // arrSelected [0]['item0'] 
            // arrSelected [1]['item1']
            // arrSelected [2]['item2']
        });
        

        【讨论】:

        • 代码通常很有帮助,但伴随它的解释往往是人们所追求的。也许考虑添加一些东西?
        • 是的。我的解释在 Ravshanbek 的代码片段中。我在评论代码的功能。下次我会记住不要在代码中执行此操作。谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        相关资源
        最近更新 更多