<!DOCTYPE html><html><body><form action="form-action.php">搜索: <input type="search" id="mySearch" name="filter"><input type="submit" value="提交"></form><p>点击 "点我" 按钮设置搜索字段 required 属性。</p><p><strong>提示:</strong> 在点击 “点我” 按钮前后点击 "提交" 按钮查看效果</p><button onclick="myFunction()">点我</button><p id="demo"></p><script>function myFunction() {document.getElementById("mySearch").required = true;document.getElementById("demo").innerHTML = " required 属性已设置。表单提交前搜索字段为必填字段。 ";}</script></body></html>