{"id":2065,"date":"2025-02-26T23:25:57","date_gmt":"2025-02-26T15:25:57","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2065"},"modified":"2025-02-26T23:25:57","modified_gmt":"2025-02-26T15:25:57","slug":"javascript-%e8%a1%a8%e5%8d%95","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e8%a1%a8%e5%8d%95\/","title":{"rendered":"JavaScript \u8868\u5355"},"content":{"rendered":"\n<p>\u5728\u7f51\u9875\u4e2d\uff0c\u8868\u5355\uff08<code>&lt;form&gt;<\/code>\uff09\u662f\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u91cd\u8981\u5143\u7d20\u4e4b\u4e00\u3002JavaScript \u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\u3001\u9a8c\u8bc1\u548c\u5904\u7406\u8868\u5355\u6570\u636e\u3002\u901a\u8fc7\u7ed3\u5408 HTML \u8868\u5355\u548c JavaScript\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u4ea4\u4e92\u529f\u80fd\uff0c\u5982\u8868\u5355\u9a8c\u8bc1\u3001\u6570\u636e\u63d0\u4ea4\u3001\u7528\u6237\u63d0\u793a\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u83b7\u53d6\u8868\u5355\u5143\u7d20<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u8868\u5355\u5143\u7d20\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7 <code>document.getElementById()<\/code>\u3001<code>document.getElementsByName()<\/code>\u3001<code>document.getElementsByTagName()<\/code> \u6216 <code>document.querySelector()<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u83b7\u53d6\u8868\u5355\u5143\u7d20<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;input type=\"text\" name=\"username\" id=\"username\" \/&gt;\n  &lt;input type=\"password\" name=\"password\" id=\"password\" \/&gt;\n  &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var form = document.getElementById(\"myForm\");\n  var username = document.getElementById(\"username\");\n  var password = document.getElementById(\"password\");\n\n  form.addEventListener(\"submit\", function(event) {\n    event.preventDefault();\n    alert(\"Form submitted!\");\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u8868\u5355\u9a8c\u8bc1<\/strong><\/h3>\n\n\n\n<p>\u8868\u5355\u9a8c\u8bc1\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7b26\u5408\u8981\u6c42\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u6765\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u9a8c\u8bc1\u3002<code>submit<\/code> \u4e8b\u4ef6\u53ef\u4ee5\u5728\u8868\u5355\u63d0\u4ea4\u524d\u89e6\u53d1\u9a8c\u8bc1\u51fd\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u8868\u5355\u9a8c\u8bc1<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\" onsubmit=\"return validateForm()\"&gt;\n  &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n  &lt;input type=\"text\" id=\"email\" name=\"email\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  function validateForm() {\n    var email = document.getElementById(\"email\").value;\n    if (email == \"\") {\n      alert(\"Email must be filled out!\");\n      return false;\n    }\n    return true;\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>validateForm()<\/code> \u51fd\u6570\u68c0\u67e5\u7528\u6237\u662f\u5426\u586b\u5199\u4e86\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5982\u679c\u672a\u586b\u5199\uff0c\u5f39\u51fa\u63d0\u793a\u5e76\u963b\u6b62\u8868\u5355\u63d0\u4ea4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u83b7\u53d6\u548c\u8bbe\u7f6e\u8868\u5355\u503c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u83b7\u53d6\u6216\u8bbe\u7f6e\u8868\u5355\u5b57\u6bb5\u7684\u503c\u3002\u5e38\u7528\u7684\u5c5e\u6027\u6709 <code>value<\/code> \u548c <code>checked<\/code>\uff08\u7528\u4e8e\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u83b7\u53d6\u8868\u5355\u503c<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form&gt;\n  &lt;label for=\"username\"&gt;Username:&lt;\/label&gt;\n  &lt;input type=\"text\" id=\"username\" name=\"username\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var username = document.getElementById(\"username\").value;\n  console.log(username);  \/\/ \u83b7\u53d6\u6587\u672c\u6846\u7684\u503c\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u8bbe\u7f6e\u8868\u5355\u503c<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form&gt;\n  &lt;label for=\"username\"&gt;Username:&lt;\/label&gt;\n  &lt;input type=\"text\" id=\"username\" name=\"username\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  document.getElementById(\"username\").value = \"john_doe\";  \/\/ \u8bbe\u7f6e\u6587\u672c\u6846\u7684\u503c\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u8868\u5355\u63d0\u4ea4<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7 JavaScript\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>submit()<\/code> \u65b9\u6cd5\u6765\u7a0b\u5e8f\u5316\u5730\u63d0\u4ea4\u8868\u5355\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u76f4\u63a5\u8c03\u7528 <code>submit()<\/code> \u65b9\u6cd5\u4e0d\u4f1a\u89e6\u53d1 <code>onsubmit<\/code> \u4e8b\u4ef6\uff0c\u56e0\u6b64\u5728\u81ea\u52a8\u63d0\u4ea4\u8868\u5355\u65f6\u4e0d\u4f1a\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u81ea\u52a8\u63d0\u4ea4\u8868\u5355<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;input type=\"text\" name=\"username\" id=\"username\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  document.getElementById(\"myForm\").submit();  \/\/ \u81ea\u52a8\u63d0\u4ea4\u8868\u5355\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u963b\u6b62\u8868\u5355\u63d0\u4ea4<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>event.preventDefault()<\/code> \u6765\u963b\u6b62\u8868\u5355\u7684\u9ed8\u8ba4\u63d0\u4ea4\u884c\u4e3a\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u63d0\u4ea4\u8868\u5355\u524d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff08\u5982\u9a8c\u8bc1\u8868\u5355\u6570\u636e\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u963b\u6b62\u8868\u5355\u63d0\u4ea4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;input type=\"text\" name=\"username\" id=\"username\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var form = document.getElementById(\"myForm\");\n\n  form.addEventListener(\"submit\", function(event) {\n    event.preventDefault();  \/\/ \u963b\u6b62\u9ed8\u8ba4\u63d0\u4ea4\u884c\u4e3a\n    alert(\"Form not submitted!\");\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u5904\u7406\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae<\/strong><\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae\uff0cJavaScript \u4f7f\u7528 <code>checked<\/code> \u5c5e\u6027\u6765\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u88ab\u9009\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u590d\u9009\u6846<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form&gt;\n  &lt;label for=\"agree\"&gt;I agree to the terms and conditions&lt;\/label&gt;\n  &lt;input type=\"checkbox\" id=\"agree\" name=\"agree\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var checkbox = document.getElementById(\"agree\");\n\n  document.querySelector(\"form\").addEventListener(\"submit\", function(event) {\n    if (!checkbox.checked) {\n      alert(\"You must agree to the terms and conditions!\");\n      event.preventDefault();  \/\/ \u963b\u6b62\u8868\u5355\u63d0\u4ea4\n    }\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5355\u9009\u6309\u94ae<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form&gt;\n  &lt;label for=\"male\"&gt;Male&lt;\/label&gt;\n  &lt;input type=\"radio\" id=\"male\" name=\"gender\" value=\"male\" \/&gt;\n  &lt;label for=\"female\"&gt;Female&lt;\/label&gt;\n  &lt;input type=\"radio\" id=\"female\" name=\"gender\" value=\"female\" \/&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var radios = document.getElementsByName(\"gender\");\n\n  document.querySelector(\"form\").addEventListener(\"submit\", function(event) {\n    var selectedGender = Array.from(radios).find(radio =&gt; radio.checked);\n    if (!selectedGender) {\n      alert(\"Please select a gender!\");\n      event.preventDefault();  \/\/ \u963b\u6b62\u8868\u5355\u63d0\u4ea4\n    }\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u52a8\u6001\u66f4\u65b0\u8868\u5355\u5185\u5bb9<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7 JavaScript\uff0c\u4f60\u53ef\u4ee5\u52a8\u6001\u5730\u4fee\u6539\u8868\u5355\u4e2d\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u5b57\u6bb5\uff0c\u6216\u8005\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u66f4\u65b0\u5176\u4ed6\u90e8\u5206\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u52a8\u6001\u6dfb\u52a0\u8f93\u5165\u5b57\u6bb5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"dynamicForm\"&gt;\n  &lt;input type=\"text\" name=\"item\" \/&gt;\n  &lt;button type=\"button\" onclick=\"addInput()\"&gt;Add another item&lt;\/button&gt;\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  function addInput() {\n    var newInput = document.createElement(\"input\");\n    newInput.type = \"text\";\n    newInput.name = \"item\";\n    document.getElementById(\"dynamicForm\").insertBefore(newInput, event.target);\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>JavaScript \u5728\u8868\u5355\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5e38\u89c1\u7684\u7528\u6cd5\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6\u548c\u8bbe\u7f6e\u8868\u5355\u5143\u7d20\u7684\u503c\u3002<\/li>\n\n\n\n<li>\u63d0\u4ea4\u8868\u5355\u53ca\u963b\u6b62\u8868\u5355\u7684\u9ed8\u8ba4\u63d0\u4ea4\u884c\u4e3a\u3002<\/li>\n\n\n\n<li>\u8868\u5355\u9a8c\u8bc1\u3001\u52a8\u6001\u66f4\u65b0\u8868\u5355\u5185\u5bb9\u3001\u64cd\u4f5c\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u5668\u5904\u7406\u8868\u5355\u7684\u5404\u79cd\u4ea4\u4e92\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u638c\u63e1\u8fd9\u4e9b\u8868\u5355\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4f60\u5c31\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u4f18\u5316\u7f51\u9875\u4e2d\u7684\u8868\u5355\u4ea4\u4e92\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f51\u9875\u4e2d\uff0c\u8868\u5355\uff08&lt;form&gt;\uff09\u662f\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u91cd\u8981\u5143\u7d20\u4e4b\u4e00\u3002JavaScript \u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\u3001\u9a8c\u8bc1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2065","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2065"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2065\/revisions"}],"predecessor-version":[{"id":2066,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2065\/revisions\/2066"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}