我正在使用以下脚本,如果某人从多个下拉列表中选择了相同的值,则会显示弹出错误。效果很好,但是在显示弹出窗口后,仍然会发生重复选择。它应该防止这种情况发生。
$(document).ready(function () {
$('select').change(function () {
if ($('select option[value="' + $(this).val() + '"]:selected').length > 1) {
alert('You have already selected this option previously - please choose another.')
}
});
});
答案 0 :(得分:3)
您可以切换到默认选项:
$(this).val('-1').change();
希望这有帮助。
$(document).ready(function () {
$('select').change(function () {
if ($('select option[value="' + $(this).val() + '"]:selected').length > 1) {
$(this).val('-1').change();
alert('You have already selected this option previously - please choose another.')
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value='-1'>Choose an option</option>
<option value='1'>option 1</option>
<option value='2'>option 2</option>
<option value='3'>option 3</option>
</select>
<select>
<option value='-1'>Choose an option</option>
<option value='4'>option 4</option>
<option value='1'>option 1</option>
<option value='5'>option 5</option>
</select>
&#13;
答案 1 :(得分:1)
一个选项是赋值0以强制选择新值,类似这样的
jQuery(document).ready(function () {
$('select').change(function () {
if ($('select option[value="' + $(this).val() + '"]:selected').length > 1)
{
$(this).val(0);
alert('You have already selected this company - please choose another.')
}
});
});