提交嵌套表单,奇怪的行为

时间:2022-01-21 17:26:26

I have a ad banner that has a form and this is used by 3rd party site owners that grab this code and have this banner on there site.

我有一个带有表单的广告横幅,第三方网站所有者使用此横幅抓取此代码并在该网站上显示此横幅。

Since some sites that use this banner have a <form> wrapping there entire site i have found that the banners form doesn't submit it rather submits the outer form.

由于一些使用此横幅的网站在整个网站上都有一个

包装,我发现横幅表单不提交,而是提交外部表单。

Here is the code:

  <form onSubmit="return alert('outer form submitted')">

    <!-- only if i add this empty form it submits the inner -->
         <form></form>
    <!-- End Empty form -->

    <!-- inner form -->
      <form onSubmit="return alert('inner form submitted')" > 
           <input type="submit" /> 
      </form> 
    <!--end  inner form -->

 </form> 

As you can see only if i add a empty <form> before the inner <form> it alert (submits) the inner, otherwise it submits the outer.

正如你所看到的那样,只有在内部

之前添加一个空的它才会提醒(提交)内部,否则它会提交外部。

what is the best way to code a banner ad for a site with a form?

为具有表单的网站编写横幅广告的最佳方法是什么?

1 个解决方案

#1


9  

Having a <form> inside a <form> is malformed HTML, and all bets are off how any browser/user-agent/javascript-engine/dom implementation is going to handle it. Expect wildly different or equal results between different visitors. Just don't have a <form> in a <form>. Period :)

中有一个是格式错误的HTML,所有的赌注都是关闭任何浏览器/用户代理/ javascript-engine / dom实现将如何处理它。不同访客之间的结果会有很大差异或相同。只是在中没有。期间:)

Most likely scenario in your observed behavior: starting a <form> tag inside a form is probably 'discarded' as invalid, including any actions/events (but probably not <input>s), the first </form> closes the 'main' form.

您观察到的行为中最有可能发生的情况:在表单中启动

标记可能被“丢弃”为无效,包括任何操作/事件(但可能不是 ),第一个 关闭'main '形式。

#1


9  

Having a <form> inside a <form> is malformed HTML, and all bets are off how any browser/user-agent/javascript-engine/dom implementation is going to handle it. Expect wildly different or equal results between different visitors. Just don't have a <form> in a <form>. Period :)

中有一个是格式错误的HTML,所有的赌注都是关闭任何浏览器/用户代理/ javascript-engine / dom实现将如何处理它。不同访客之间的结果会有很大差异或相同。只是在中没有。期间:)

Most likely scenario in your observed behavior: starting a <form> tag inside a form is probably 'discarded' as invalid, including any actions/events (but probably not <input>s), the first </form> closes the 'main' form.

您观察到的行为中最有可能发生的情况:在表单中启动

标记可能被“丢弃”为无效,包括任何操作/事件(但可能不是 ),第一个 关闭'main '形式。