首页 > 解决方案 > Is there a way to have one single form HTML element that can send information to numerous URL's?

问题描述

I'm designing a front end for Google in my class, but the only issue I'm having is that my <form> element where a user can type in keywords to send to google and get the results on their page only supports one URL to send information to.

Is there a way I can add a feature (preferably a button) to send that info to the "I'm feeling lucky" URL without having to create a separate form, using plain HTML?

I've attached some of my code if it helps.

 <form action="https://google.com/search" name="Search" class="search" >
   <input type="text" name="q" placeholder="Search Google or type a URL" id="query">
   <input type="submit" value="Search" class="submit">
 </form>

标签: htmlforms

解决方案


While this is typically handled with JavaScript (or server-side logic), it is indeed possible to achieve this with just HTML, as HTML5 introduces the formaction attribute on <input type="submit"> (and type="image"), which allows you to modify the submission location.

This can be used to submit to two different locations based on button click:

<form>
  <input type="submit" formaction="/one" value="Submit To URL One">
  <input type="submit" formaction="/two" value="Submit To URL Two">
</form>

This is supported by all modern browsers.

Browsers


推荐阅读