首页 > 解决方案 > 必填字段在 Select2 选择框中不起作用

问题描述

我正在使用 Select2 来自定义选择框。选择框是必填字段。但是,它没有按预期工作。我可以知道为什么会这样吗?谢谢。

$(document).ready(function() {
    $('.js-example-basic-single').select2({
        dropdownAutoWidth: true,
        dropdownParent: $('#parent')
    });
});
<!DOCTYPE html>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<form enctype="multipart/form-data" id="testForm" method="post" name="testForm" action="#">
<table width="200">
<tr>
<td width="35" />
<td class="label" width="160">&nbsp;Authorized By:</td>
<td>
<div id='parent'>
&nbsp;<select class="js-example-basic-single" name="friend" style="width:60%" required>
<option />&nbsp;
<option value="John">John</option>
<option value="Kate">Kate</option>
<option value="Zoe">Zoe</option>
<option value="Peter">Peter</option>
</select>
</div>
</tr>
<tr>
<td align="center" colspan="3" height="35"><button class="button" type="submit">Submit</button></td>
</tr>
</table>
</form>
</HEAD>
</HTML>

标签: jqueryjquery-select2

解决方案


推荐阅读