使用 Javascript 在表单提交时重定向用户时出现问题

发布于 2024-09-11 08:07:38 字数 900 浏览 5 评论 0原文

我有一个网页,上面有一个表格。该表单包含一个按钮和一个文本框。单击按钮或提交表单时,我想从文本框中获取文本并将其附加到网址中。代码如下所示:

<form 
 name="askaquestion"  
 id="ask-a-question"  
 onSubmit="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value"  
    >  
 <input  
  type="submit"  
  name="submit_question"  
  value="Submit"  
  class="submit"  
  onClick="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value"  
 />  
 <input  
  type="text"  
  name="question_ask"  
  value="What's Your Question?"  
  class="inputsmall"  
  id="ask_us_a_question_textbox"  
 />  
</form>  

问题是这有时有效,有时它会将我带到此 URL:

http://dev/fs?submit_question=Submit&question_ask=help&hiddenInputToUpdateATBuffer_CommonToolkitScripts=1#

我不确定为什么会发生这种情况。该表单使用 jQuery 淡入淡出,这可能是一个问题吗?有没有更好的方法来实现所描述的行为?

I have a web page on which I have a form. This form contains a button and a textbox. When the button is clicked or when the form is submitted I would like to take the text from the text box and append it to a url. The code looks like this:

<form 
 name="askaquestion"  
 id="ask-a-question"  
 onSubmit="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value"  
    >  
 <input  
  type="submit"  
  name="submit_question"  
  value="Submit"  
  class="submit"  
  onClick="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value"  
 />  
 <input  
  type="text"  
  name="question_ask"  
  value="What's Your Question?"  
  class="inputsmall"  
  id="ask_us_a_question_textbox"  
 />  
</form>  

The problem is that this works sometimes and sometimes it takes me to this URL:

http://dev/fs?submit_question=Submit&question_ask=help&hiddenInputToUpdateATBuffer_CommonToolkitScripts=1#

I am not sure why this is happening. The form is in a using jQuery to fade in and out, could this be an issue? Is there a better way of achieving the described behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-09-18 08:07:39

当用户单击提交按钮时,执行 onClick 操作后将触发“提交”操作。为了防止发生提交操作,您需要执行 Gert 所说的操作,在

为什么不直接使用

,这样数据就会发送到您指定的网址?

如果您确实希望将数据发送到两个不同的位置,那么最好在第一个处理程序中执行第二组请求,即表单操作指定的内容。

一些有用的链接:
http://www.w3schools.com/tags/tag_form.asp
http://bytes.com/topic/javascript/答案/809181-noobie-onclick-confirm-return-false

When users click on the submit button, a "submit" action would be triggered, after performing the onClick action. To prevent the submit action from happening, you need to do what Gert said, return false on either <input onClick="return false"> or <form onSubmit="return false">.

Why don't you just use <form action="http://www.someurl.com/app/">, so the data would be sent to that url you specified?

If you really want the data to be sent to two different places, it's probably better to do the second set of requests in your first handler, i.e. what the form action specifies.

Some useful links:
http://www.w3schools.com/tags/tag_form.asp
http://bytes.com/topic/javascript/answers/809181-noobie-onclick-confirm-return-false

我家小可爱 2024-09-18 08:07:38

return false; 添加到 onSubmit

onSubmit="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value; return false;"

这将停止表单提交(这就是为什么你得到 http://dev/fs?submit_question... - 表示您的页面名为 fs)。

Add return false; to the onSubmit:

onSubmit="window.location.href='http://www.someurl.com/app/' + document.askaquestion.question_ask.value; return false;"

This stops the form submission (that's why you got http://dev/fs?submit_question... - which indicates that your page is named fs).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文