是否可以仅从<a>标签进行POST ?我知道锚标签通常只适用于GET,而且我知道我可以使用javascript来执行此操作(就像在JavaScript提交请求中一样提交表单)但对我来说这似乎有点混乱.有没有办法用直接的HTML做到这一点?
Ry-*_*Ry- 32
不是,不是.但是,您可以执行以下操作:
<form action="theUrl" method="POST">
<input type="hidden" name="param1" value="val" />
<input type="hidden" name="param2" value="val2" />
<a href="#" onclick="this.parentNode.submit()">Go to that link!</a>
</form>
不过,你应该找到一个更好的方法.这个不会优雅地降级.
Ode*_*ded 26
没有办法只使用HTML POST的a元素.
从这个DTD片段(HTML 4.01规范)可以看出:
<!ELEMENT A - - (%inline;)* -(A)       -- anchor -->
<!ATTLIST A
  %attrs;                              -- %coreattrs, %i18n, %events --
  charset     %Charset;      #IMPLIED  -- char encoding of linked resource --
  type        %ContentType;  #IMPLIED  -- advisory content type --
  name        CDATA          #IMPLIED  -- named link end --
  href        %URI;          #IMPLIED  -- URI for linked resource --
  hreflang    %LanguageCode; #IMPLIED  -- language code --
  rel         %LinkTypes;    #IMPLIED  -- forward link types --
  rev         %LinkTypes;    #IMPLIED  -- reverse link types --
  accesskey   %Character;    #IMPLIED  -- accessibility key character --
  shape       %Shape;        rect      -- for use with client-side image maps --
  coords      %Coords;       #IMPLIED  -- for use with client-side image maps --
  tabindex    NUMBER         #IMPLIED  -- position in tabbing order --
  onfocus     %Script;       #IMPLIED  -- the element got the focus --
  onblur      %Script;       #IMPLIED  -- the element lost the focus --
  >
没有其控制是否使用属性POST或GET与a元件.
你必须对它进行脚本,如果你想滥用语义.
SLa*_*aks 10
您可以使用CSS <input type="submit">看起来像超链接.
基本上,您不能对 POST 请求使用锚标记。但是,有一个简单的技巧可以实现这一点。
<form id="myform" method="post" action="target.html"></form>
<a onclick="document.getElementById('myform').submit();">Submit to action</a>
我已经对此进行了测试,并且已经在我们的生产代码中使用了它。
如果它为某人服务:
<a href="/your-route"
    onclick="event.preventDefault();
    document.getElementById('magic-form').submit();">
    Magic Action
</a>
<form id="magic-form" action="/your-route" 
      method="POST" style="display: none;">
      {{ csrf_field() }} <!-- from your framework -->
      <input type="hidden" name="field1" value="value1" />
      <!-- other fields -->
</form>