使用 Javascript 在表单提交时重定向用户时出现问题
我有一个网页,上面有一个表格。该表单包含一个按钮和一个文本框。单击按钮或提交表单时,我想从文本框中获取文本并将其附加到网址中。代码如下所示:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用户单击提交按钮时,执行 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
将
return false;
添加到onSubmit
:这将停止表单提交(这就是为什么你得到
http://dev/fs?submit_question...
- 表示您的页面名为fs
)。Add
return false;
to theonSubmit
:This stops the form submission (that's why you got
http://dev/fs?submit_question...
- which indicates that your page is namedfs
).