0Pricing
Browser Extensions Development (Chrome & Edge) · บทเรียน

การส่งแบบฟอร์มด้วยโปรแกรม

ทำให้การกรอกและส่งแบบฟอร์มบนหน้าเว็บเป็นแบบอัตโนมัติ ช่วยลดความยุ่งยากของงานป้อนข้อมูลซ้ำ ๆ สำหรับผู้ใช้

การส่งแบบฟอร์มด้วยโปรแกรม เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Automating Form Submissions

Browser extensions can do more than just read web pages; they can also interact with them! One powerful capability is programmatically filling out and submitting forms.

This means your extension can automatically enter text, select options, and click buttons on a web form, saving users time and effort on repetitive tasks.

Finding Forms and Fields

Before you can interact with a form, your content script needs to locate it and its input fields on the web page. You'll typically use standard DOM (Document Object Model) methods for this.

  • Use document.querySelector() or document.getElementById() to target elements.
  • Inspect the page's HTML to find unique IDs or classes for form elements.
(function() {
  // Find a form by its ID
  const loginForm = document.getElementById('login-form');
  // Find an input field by its name attribute within that form
  const usernameField = document.querySelector('#login-form input[name="username"]');
  
  if (loginForm && usernameField) {
    console.log("Form and username field found!");
  } else {
    console.log("Could not find form or username field.");
  }
})();

Filling Text Fields

To enter text into <input type="text"> or <textarea> elements, you simply set their value property. This property directly controls the content displayed in the field.

(function() {
  // Assume an input field like <input type="text" id="myTextInput">
  const myTextInput = document.getElementById('myTextInput');
  if (myTextInput) {
    myTextInput.value = "CoddyKit Demo";
    console.log("Text input value set to: " + myTextInput.value);
  } else {
    console.log("Text input field not found.");
  }
})();

Handling Checkboxes & Radios

For checkboxes and radio buttons, you modify their checked property. Setting it to true selects the element, while false deselects it.

  • Checkboxes: Can be individually toggled.
  • Radio Buttons: Setting checked = true on one in a group will automatically uncheck others in that same group.
(function() {
  // Assume <input type="checkbox" id="agreeTerms">
  const agreeCheckbox = document.getElementById('agreeTerms');
  if (agreeCheckbox) {
    agreeCheckbox.checked = true;
    console.log("Terms agreed: " + agreeCheckbox.checked);
  }

  // Assume <input type="radio" name="choice" id="choiceA">
  const choiceARadio = document.getElementById('choiceA');
  if (choiceARadio) {
    choiceARadio.checked = true;
    console.log("Choice A selected: " + choiceARadio.checked);
  }
})();

Managing Dropdown Selections

To select an option in a <select> dropdown, you set the value property of the <select> element to the value of the desired <option>.

(function() {
  // Assume <select id="countrySelect"><option value="us">USA</option><option value="ca">Canada</option></select>
  const countrySelect = document.getElementById('countrySelect');
  if (countrySelect) {
    countrySelect.value = "ca"; // Selects the option with value="ca"
    console.log("Selected country: " + countrySelect.value);
  } else {
    console.log("Country select dropdown not found.");
  }
})();

Triggering Change Events

Sometimes, simply setting an element's value or checked property isn't enough. Many web forms use JavaScript to react to user input, often listening for 'change' or 'input' events.

If a form doesn't behave as expected after setting values, you might need to programmatically dispatch these events to simulate user interaction.

(function() {
  // Assume <input type="text" id="quantityInput">
  const quantityInput = document.getElementById('quantityInput');
  if (quantityInput) {
    quantityInput.value = "5";
    console.log("Value set. Now dispatching change event...");
    // Dispatch a 'change' event to trigger any associated JS listeners
    quantityInput.dispatchEvent(new Event('change', { bubbles: true }));
    console.log("Change event dispatched.");
  } else {
    console.log("Quantity input field not found.");
  }
})();

Programmatically Submitting Forms

Once all the fields are filled, you need to submit the form. There are two primary ways to do this:

  • Click the submit button: Find the <button type="submit"> or <input type="submit"> and call its .click() method.
  • Call form.submit(): Get a reference to the <form> element and call its .submit() method directly. This bypasses any click handlers on the submit button.
(function() {
  // Assume <form id="dataForm">...</form> and <button type="submit" id="sendBtn">Send</button>
  const dataForm = document.getElementById('dataForm');
  const submitButton = document.getElementById('sendBtn');
  
  if (dataForm) {
    console.log("Submitting form directly using .submit()...");
    dataForm.submit(); 
  } else if (submitButton) {
    console.log("Clicking submit button...");
    submitButton.click();
  } else {
    console.log("Form or submit button not found.");
  }
})();

Handling Dynamic Forms

Some forms or input fields might not be immediately present when your content script loads. They could be added dynamically by the page's JavaScript after an AJAX call or user interaction.

For these scenarios, you might need to:

  • Use setTimeout or setInterval to poll for element existence.
  • Employ a MutationObserver to watch for changes in the DOM that indicate the form is ready.

Robust Form Automation

When automating forms, always consider robustness and error handling:

  • Check for element existence: Always verify an element exists before trying to interact with it.
  • Use try...catch: Wrap critical operations in try...catch blocks to handle unexpected errors.
  • Be specific with selectors: Use unique IDs or specific attribute selectors to avoid targeting the wrong elements.

Form Automation Check

You're building an extension to autofill a registration form. Which JavaScript code snippet correctly sets the value of a text input with id='usernameField' to 'newuser123'?

Recap: Form Submission

You've learned how to programmatically interact with web forms, a powerful feature for browser extensions!

  • Locate forms and inputs using DOM selectors.
  • Set .value for text fields and dropdowns.
  • Set .checked for checkboxes and radio buttons.
  • Dispatch events like 'change' if needed.
  • Submit forms by clicking a button or calling .submit().

This skill allows you to build extensions that greatly enhance user productivity by automating repetitive data entry.

คำถามที่พบบ่อย

บทเรียน “การส่งแบบฟอร์มด้วยโปรแกรม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การส่งแบบฟอร์มด้วยโปรแกรม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การส่งแบบฟอร์มด้วยโปรแกรม”

ทำให้การกรอกและส่งแบบฟอร์มบนหน้าเว็บเป็นแบบอัตโนมัติ ช่วยลดความยุ่งยากของงานป้อนข้อมูลซ้ำ ๆ สำหรับผู้ใช้ คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การส่งแบบฟอร์มด้วยโปรแกรม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม

ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดการและควบคุมแท็บ
  2. การส่งแบบฟอร์มด้วยโปรแกรม
  3. การทำให้การโต้ตอบของผู้ใช้เป็นอัตโนมัติ
  4. การจัดตารางงานด้วย API Alarms
← กลับไปที่ Browser Extensions Development (Chrome & Edge)