เราทำให้ formaction ทำงานนอกแท็ก ได้ แอตทริบิวต์ formaction ใช้เพื่อระบุ URL ที่ส่งมากกว่าหนึ่งรายการสำหรับแบบฟอร์มเดียว เมื่อคุณส่งแบบฟอร์ม เว็บเบราว์เซอร์จะตรวจสอบแอตทริบิวต์ formaction ก่อน
หากไม่มี formaction เว็บเบราว์เซอร์จะค้นหาแอตทริบิวต์ action ในองค์ประกอบของแบบฟอร์ม
ตัวอย่าง
นี่คือตัวอย่าง รูปแบบ แอตทริบิวต์ที่มีปุ่มส่งสามปุ่มที่แตกต่างกัน -
<!DOCTYPE html> <html> <head> <title>HTML formaction attribute</title> </head> <body> <form method="post"> <input type = "text" name="name"/><br> <button type = "submit" formaction = "btn1.php">Button1</button> <button type = "submit" formaction = "btn2.php">Button2</button> <button type = "submit" formaction = "btn3.php">Button3</button> </form> </body> </html>
ใช่ แอตทริบิวต์ formaction จะไม่ทำงานนอกองค์ประกอบของแบบฟอร์ม แต่คุณยังสามารถปล่อยให้ทำงานอย่างถูกต้องด้วยวิธีต่อไปนี้ -
ตัวอย่าง
คุณสามารถวางปุ่มและใช้แอตทริบิวต์ formaction ภายนอกแบบฟอร์มได้อย่างง่ายดาย โดยใช้ค่ารหัสแบบฟอร์มที่เกี่ยวข้อง
<!DOCTYPE html> <html> <head> <title>HTML formaction attribute</title> </head> <body> <form method="post" id="newForm"> <input type="text" name="name"/> </form> <button type="submit" formaction="btn1.php" form="newForm">Button1</button> <button type="submit" formaction="btn2.php" form="newForm">Button2</button> <button type="submit" formaction="btn3.php" form="newForm">Button3</button> </body> </html>