简体   繁体   English

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

[英]Problem when using Javascript to redirect user on form submit

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.单击按钮或提交表单时,我想从文本框中获取文本并将其附加到 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:问题是这有时会起作用,有时它会将我带到此 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?表单使用 jQuery 淡入淡出,这可能是一个问题吗? Is there a better way of achieving the described behaviour?有没有更好的方法来实现所描述的行为?

Add return false;添加return false; to the onSubmit :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 ).这会停止表单提交(这就是为什么你得到http://dev/fs?submit_question... - 这表明你的页面被命名为fs )。

When users click on the submit button, a "submit" action would be triggered, after performing the onClick action.当用户点击提交按钮时,会在执行 onClick 操作后触发“提交”操作。 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"> .为了防止提交操作发生,您需要按照 Gert 所说的去做,在<input onClick="return false"><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?为什么不直接使用<form action="http://www.someurl.com/app/"> ,这样数据就会被发送到您指定的网址?

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, ie what the form action specifies.如果您真的希望将数据发送到两个不同的地方,最好在第一个处理程序中执行第二组请求,即表单操作指定的内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM