要使用 jQuery 实现单击提交按钮时关闭窗口的功能,可以使用该window.close()方法。但是,请注意,使用关闭窗口的能力window.close()取决于浏览器设置,并且在许多现代浏览器中,除非使用 JavaScript 打开窗口,否则它可能无法工作。
下面是一个简单的示例,说明如何使用 jQuery 在单击提交按钮时关闭当前窗口:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Close Window Example</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> </head> <body> <form id="myForm"> <!-- Your form fields go here --> <input type="submit" value="Submit"> </form> <script> $(document).ready(function() { // Attach a submit event handler to the form $('#myForm').submit(function() { // Close the current window when the form is submitted window.close(); // Prevent the default form submission (optional, depending on your needs) return false; }); }); </script> </body> </html>
myForm在此示例中,当提交具有 ID 的表单时,submit将执行事件处理程序内的 jQuery 代码。它调用window.close()关闭当前窗口。该return false;语句是可选的,并阻止默认表单提交,这可能是必要的,具体取决于您的具体用例。
请记住,使用关闭窗口的功能window.close()受到浏览器安全限制的限制,并且它可能无法在所有情况下工作。此外,以编程方式关闭窗口可能被认为是侵入性的,并且某些浏览器可能出于安全原因而阻止它。