设置最小值以在select2插件中选择

时间:2022-11-27 19:18:33

Is there a way by which I can validate select two only to allow a minimum number of selection length as it has for maximumSelectionLength.

有没有办法我可以验证选择2只允许最小数量的选择长度,因为它具有maximumSelectionLength。

I have tried minimumSelectionLength but it does not work for me :

我尝试过minimumSelectionLength,但它对我不起作用:

<link rel="stylesheet" type="text/css" href="select2/select2.min.css" />
<script src="select2/select2.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
      $(".selecte").select2();
      maximumSelectionLength: 15;

    });
</script>

So I want the button not to submit until the user selects up to like 3 items.

所以我希望按钮不提交,直到用户选择最多喜欢3项。

Any ideas or suggestion.

任何想法或建议。

3 个解决方案

#1


3  

select2 plugin don't have any attribute for this purpose so you could validate the select element want in submit event make you condition then submit when it's achieved :

select2插件没有任何属性用于此目的,因此您可以验证select元素想要在提交事件中使您有条件然后在实现时提交:

$('form').on('submit', function(){
     var minimum = 3;

     if($(".selecte").select2('data').length>=minimum)
         return true;
     else 
         return false;
});

Hope this helps.

希望这可以帮助。


$(".selecte").select2();

$('form').on('submit', function(){
     var minimum = 2;

     if($(".selecte").select2('data').length>=minimum){
         alert('Submited...')
         return true;
     }else {
     	 alert('Please shoose at least '+minimum+' item(s)')
         return false;
     }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script>
<form>
  <select multiple class="selecte" style="width:300px">
    <option value="AL">Alabama</option>
    <option value="Am">Amalapuram</option>
    <option value="An">Anakapalli</option>
    <option value="Ak">Akkayapalem</option>
    <option value="WY">Wyoming</option>
  </select>
  
  <br>
  
  <input type='submit' />
</form>

#2


2  

    var min = 1; 
    $('input[type="submit"]').on('click', function(event){
        if ($('#group_select option:selected').size() < min) {
            alert("Need at least one group");
            event.preventDefault();
        }
    });

Popup a alert if the min requirement is not satisfied.

如果不满足最小要求,则弹出警报。

#3


1  

Use .val() in your JS to get the value of the input, after that you can use regex in an if statement that if it returns true ( meets the value requirement set by regex ) then it will submit. You can use console.log() the value, to see what data you are working with. (regex101.com is a great place to build your regex code.)

在JS中使用.val()来获取输入的值,之后你可以在if语句中使用regex,如果它返回true(满足regex设置的值要求),那么它将提交。您可以使用console.log()值来查看您正在使用的数据。 (regex101.com是构建正则表达式代码的好地方。)

#1


3  

select2 plugin don't have any attribute for this purpose so you could validate the select element want in submit event make you condition then submit when it's achieved :

select2插件没有任何属性用于此目的,因此您可以验证select元素想要在提交事件中使您有条件然后在实现时提交:

$('form').on('submit', function(){
     var minimum = 3;

     if($(".selecte").select2('data').length>=minimum)
         return true;
     else 
         return false;
});

Hope this helps.

希望这可以帮助。


$(".selecte").select2();

$('form').on('submit', function(){
     var minimum = 2;

     if($(".selecte").select2('data').length>=minimum){
         alert('Submited...')
         return true;
     }else {
     	 alert('Please shoose at least '+minimum+' item(s)')
         return false;
     }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script>
<form>
  <select multiple class="selecte" style="width:300px">
    <option value="AL">Alabama</option>
    <option value="Am">Amalapuram</option>
    <option value="An">Anakapalli</option>
    <option value="Ak">Akkayapalem</option>
    <option value="WY">Wyoming</option>
  </select>
  
  <br>
  
  <input type='submit' />
</form>

#2


2  

    var min = 1; 
    $('input[type="submit"]').on('click', function(event){
        if ($('#group_select option:selected').size() < min) {
            alert("Need at least one group");
            event.preventDefault();
        }
    });

Popup a alert if the min requirement is not satisfied.

如果不满足最小要求,则弹出警报。

#3


1  

Use .val() in your JS to get the value of the input, after that you can use regex in an if statement that if it returns true ( meets the value requirement set by regex ) then it will submit. You can use console.log() the value, to see what data you are working with. (regex101.com is a great place to build your regex code.)

在JS中使用.val()来获取输入的值,之后你可以在if语句中使用regex,如果它返回true(满足regex设置的值要求),那么它将提交。您可以使用console.log()值来查看您正在使用的数据。 (regex101.com是构建正则表达式代码的好地方。)