【问题标题】:What's the difference between form button-link and js button-link?表单按钮链接和js按钮链接有什么区别?
【发布时间】:2019-04-28 11:03:16
【问题描述】:

这两种将按钮变成链接的方式有什么区别:

<form method="GET" action="example.com">                
<button type="submit">button text</button>
</form>

VS

<button onclick="location.href = 'example.com';">button text</button>

效果一样,但是其中一种使用方式有什么缺陷吗?

【问题讨论】:

  • 好吧,“onclick”你激活了一个 javascript,GET 是一种将变量从一个站点传递到另一个站点的方法,它需要例如 PHP 脚本

标签: javascript html web button request


【解决方案1】:

是的。第一个将转到action URL,并将表单内的所有表单输入作为 GET 参数。

第二个是基于 javascript 事件的操作,它只转到带有您指定参数的 URL。

【讨论】:

  • 是的,我明白了。但也许在某些情况下,这些方式会产生不同的结果?
【解决方案2】:

第一个:

<form method="GET" action="example.com">                
<button type="submit">button text</button>
</form>

优势:

  1. 您可以将查询字符串数据 prams 发送到表单字段中的 url。
  2. 可以很容易地形成复杂的链接,并将复杂的数据发送到Action url。

缺点:

  1. 根据您的选择,没有任何缺点。
  2. 无法生成动态链接 Action="" 属性不支持 java 脚本代码。

第二个:

<button onclick="location.href = 'example.com';">button text</button>

优势:

  1. 您可以根据动态值变化生成链接。

    location.href = 'example.com'+document.getElementById('otherInput').value;

缺点:

  1. 您不能以这种方式发送更多数据,这仅用作基本用途。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2017-03-29
    • 2014-03-21
    相关资源
    最近更新 更多