最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Submit button on nested form submits the outer form in IE7 - Stack Overflow

programmeradmin1浏览0评论

I have the following code on my Home.aspx page:

  <form id="frmJump" method="post" action="Views/ViewsHome.aspx">
    <input name="JumpProject" /><input type="submit" value="Go" />
  </form>

However, when I click the "Go" button, the page posts back to Home.aspx rather than going to ViewsHome.aspx.

I even tried adding some script to force the form to submit:

<input name="JumpProject" onkeypress="if(event.keyCode == 13) { this.form.submit(); return false; }" />

But still even if I press ENTER, the Home.aspx page is reloaded.

The only thing I can see that might be borking things is this form is actually a child form of the main POSTBACK form that ASP.NET injects into the page.

I'm sure there's something stupid I'm missing and this post will get 800 downvotes instantly banishing me back into the n00b realm, but perhaps I haven't gotten enough sleep lately and I'm missing something stupid.

This is on IE7 and an ASP.NET 4.0 backend. I also have jQuery libraries loaded on the page incase jQuery can improve this somehow. Thanks!

I have the following code on my Home.aspx page:

  <form id="frmJump" method="post" action="Views/ViewsHome.aspx">
    <input name="JumpProject" /><input type="submit" value="Go" />
  </form>

However, when I click the "Go" button, the page posts back to Home.aspx rather than going to ViewsHome.aspx.

I even tried adding some script to force the form to submit:

<input name="JumpProject" onkeypress="if(event.keyCode == 13) { this.form.submit(); return false; }" />

But still even if I press ENTER, the Home.aspx page is reloaded.

The only thing I can see that might be borking things is this form is actually a child form of the main POSTBACK form that ASP.NET injects into the page.

I'm sure there's something stupid I'm missing and this post will get 800 downvotes instantly banishing me back into the n00b realm, but perhaps I haven't gotten enough sleep lately and I'm missing something stupid.

This is on IE7 and an ASP.NET 4.0 backend. I also have jQuery libraries loaded on the page incase jQuery can improve this somehow. Thanks!

Share Improve this question asked Nov 21, 2011 at 17:45 Mike ChristensenMike Christensen 91.4k51 gold badges219 silver badges348 bronze badges 2
  • 3 You can't nest forms in HTML. It's improper markup. – Pointy Commented Nov 21, 2011 at 17:46
  • Not only is it improper, but .Net will happily FTFY by ensuring only the outer form is submitted. ;) Which IMHO is the proper behavior. – ChrisLively Commented Nov 21, 2011 at 17:48
Add a ment  | 

3 Answers 3

Reset to default 4

The most simple explanation is that forms cannot be nested.

You might consider revising your submit logic to appropriately handle the scenario server side (via the single postback form.) Otherwise you might have to consider a route that deviates from the standards ASP.NET Webforms postback model.

You can't nest forms in HTML, so the browser will ignore the inner form tag. When you submit the form, it will submit the only form that is on the page.

If you need to post a form to a different page, you can use Javascript to either change the current form before it's sent (onclick on the button), or create a new form element, add it to the page and submit that instead.

You're not allowed to nest forms in HTML. It's not supported by most browsers anyway.

What you can do instead, since you're suffering through webforms, is to just have a Button_Click event in the C# corresponding to the submit button you're clicking, and have it do the project jump from there.

发布评论

评论列表(0)

  1. 暂无评论