简体   繁体   English

Select2:如果所有非可选的选择框都有选择,则从“提交”按钮中删除禁用的属性

[英]Select2: remove disabled attribute from submit button if all non-optional select boxes have selections

I am using Select2 to style and add functionality to my select boxes. 我正在使用Select2设置样式并向其选择框添加功能。 I want to force the users to move through the select dropdowns in order, so I start by disabling all select boxes and then enabling the first one. 我要强制用户按顺序浏览选择下拉列表,因此首先禁用所有选择框,然后再启用第一个选择框。 Each time the user makes a selection, the next select box becomes enabled. 每次用户进行选择时,将启用下一个选择框。 There will be a few selections that are optional so what I am trying to do is remove the disabled attribute from the submit button if all the non-optional selections have a value, but so far I have not been able to get it working correctly. 会有一些选择是可选的,因此,如果所有非可选选择都有值,那么我想做的就是从“提交”按钮中删除disabled属性,但到目前为止,我还无法使其正常工作。

Here is my code so far: 到目前为止,这是我的代码:

 $(function() { $('select').select2({ placeholder: '- Select -', }); $('select').prop('disabled', true); $('select:first-of-type').prop('disabled', false); $('select').on('select2:select', function(e) { $(this).nextAll('select').first().prop('disabled', false); $(this).siblings('select').not('.optional').each(function () { //console.log($(this).val()); if (!$(this).val()) { reqSelected = true; } else { reqSelected = false; } }); console.log(reqSelected); if (reqSelected === true) { $(input).prop('disabled', false); } }); }); 
 label,select { display:block; } label { margin-top:20px; } label:first-child { margin-top:0; } input { display:block; margin-top:20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/> <div clas="form"> <label>1. Select Make</label> <select> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> <label>2. Select Model</label> <select> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> <label>3. Select Color (optional)</label> <select class="optional"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> <input type="submit" disabled /> </div> 

Here's a JSFiddle if you want to play around with it. 如果您想使用它,这是一个JSFiddle

Why don't you try enabling the submit button itself with $('input[type="submit"]').prop('disabled',false) . 为什么不尝试使用$('input[type="submit"]').prop('disabled',false)启用提交按钮本身。 You could use something like $('select:last-of-type') if only want to target it when the last dropdown is changed. 如果只想在更改最后一个下拉列表时将其定位,则可以使用类似$('select:last-of-type')东西。 You can also add any logic in there with function() 您也可以使用function()在其中添加任何逻辑

You can calculate correct selections and when everything is OK then enable submit. 您可以计算正确的选择,然后在一切正常的情况下启用提交。

$(document).ready(function(){
   var correctSelections = 0,
       requiredSelections = 3; //or whatever you need
   $('div.form')on('change', 'select', function(){
       if(/*this.is(':correctSelection')*/)
           correctSelections++;
       else
           correctSelections--;
       if(correctSelections == requiredSelections)
           $('input[type="submit"]').prop('disabled',false);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM