【问题标题】:How implement I'm feeling Lucky in html?如何在 html 中实现我感到幸运?
【发布时间】:2020-11-02 06:17:59
【问题描述】:

我一直在尝试实现一个表单,该表单既可以用于正常的 Google 搜索结果,也可以用于第一个搜索结果。这是我尝试过的。

<form action="https://google.com/search">
    <input type="text" name="q" class="box">
    <br>
    <div>
        <input type="submit" value="Google Search" class="button">
        <input type="submit" value="I'm Feeling Lucky" class="button" formaction="https://google.com/search?&btnI=1">
    </div>
</form>

我知道一个事实,如果我搜索“AMD”,那么我很幸运的 URL 应该是 "https://google.com/search?q=AMD&amp;btnI=1"

但是,我无法在 URL 末尾添加 "&amp;btn1"

【问题讨论】:

  • 没有理由使用&lt;input type="submit" /&gt; 代替&lt;button type="submit"&gt;

标签: html forms


【解决方案1】:

改用&lt;button type="submit"&gt;

<form action="https://google.com/search">
    <input type="text" name="q" class="box">
    <br />
    <div>
        <button type="submit" class="button">Google Search</button>
        <button type="submit" name="btnl" value="1" class="button">I'm Feeling Lucky</button>
    </div>
</form>

【讨论】:

  • 您能否向我解释一下为什么添加名称和值属性有效。我还是个新手。
  • 为什么&lt;button&gt;&lt;input&gt; 更好?
  • @BroVic &lt;button&gt; 允许任意内容和样式化内容,例如图像和粗体文本。 &lt;button&gt; 还允许您指定与按钮文本分开的确切 value=“” 属性,而 &lt;input type=“submit”&gt; 对按钮中显示的文本和提交的值使用 value=“” 属性。
  • 好的,注意了。
【解决方案2】:

它会正常工作的。

<form action="https://google.com/search">
    <input type="text" name="q" class="box">
    <br>
    <div>
        <input type="submit" name="btnK" value="Google Search" class="button">
        <input type="submit" name="btnI" value="I am Feeling Lucky" class="button">
    </div>
</form>

【讨论】:

  • 谢谢,它起作用了,但由于我是 HTML 新手,请你解释一下它为什么起作用。
  • 当表单中有多个时,您需要为提交按钮输入名称和值,因为这是 POST 请求的工作方式。我认为您正在服用CS50W。如果你是,当你学习 django 时,你就会知道更多。
【解决方案3】:

您唯一需要做的就是为输入命名为“btnI”。

<input type="submit" value="I'm feeling lucky" name="btnI">

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多