【发布时间】:2016-06-14 18:10:11
【问题描述】:
我正在使用 Select2 为我的选择框设置样式并添加功能。我想强制用户按顺序在选择下拉列表中移动,所以我首先禁用所有选择框,然后启用第一个。每次用户进行选择时,下一个选择框就会启用。会有一些选择是可选的,所以如果所有非可选选择都有一个值,我想做的是从提交按钮中删除 disabled 属性,但到目前为止我还没有能够让它工作正确。
到目前为止,这是我的代码:
$(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>
这里有一个JSFiddle,如果你想玩的话。
【问题讨论】:
标签: javascript jquery select2