html按钮v.s. html提交? [英] html button v.s. html submit?

查看:73
本文介绍了html按钮v.s. html提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文本框和一个搜索提交按钮,当用户单击搜索提交按钮时,我想将用户重定向到url http://testsearch/results.aspx?k =< ;文本框的值k> ,例如,如果用户将StackOverflow放入文本框中,然后单击搜索按钮,我想将用户重定向到下一页,



http://testsearch/results.aspx?k = StackOverflow a>



我发现当我使用Search按钮的按钮时,它可以工作(见下面的源代码),

 < input type =textid =kname =k/> 
< input type =buttonid =Govalue =Searchonclick =location.href ='http://somemachine/Search/results.aspx?k ='+ document.getElementById( 'K')值;/>

但是当我使用提交搜索按钮时,它不起作用(请参阅下面的源代码) ?

 < input type =textid =kname =k/> 
< input type =submitid =Govalue =Searchonclick =location.href ='http://somemachine/Search/results.aspx?k ='+ document.getElementById 'K')值;/>

预先感谢,
George


$ b

 < input  

type =submitid =Govalue =Searchonclick =document.location ='http://testsearch/results.aspx?k = StackOverflow'; return false; />

语义提交按钮用于提交表单不重定向页面。你应该使用正常的按钮类型。但是,正如我所示,你也可以使用提交按钮,但这不是我想的语义。



下面这行禁止提交表单。

  return假; 

这就是您在代码中缺少的内容:)

谢谢

I have an input text box and a search submit button, and when user clicks the Search submit button, I want to redirect user to url http://testsearch/results.aspx?k=<value of text box k>, for example, if user put "StackOverflow" into text box and then clicks the search button, I want to redirect user to the following page,

http://testsearch/results.aspx?k=StackOverflow

I find when I use button for Search button, it works (see below source codes),

  <input type="text" id="k" name="k" />
  <input type="button" id="Go" value="Search" onclick="location.href = 'http://somemachine/Search/results.aspx?k='+document.getElementById('k').value;"/>

but when I use submit for Search button, it does not works (see below source codes), why?

  <input type="text" id="k" name="k" />
 <input type="submit" id="Go" value="Search" onclick="location.href = 'http://somemachine/Search/results.aspx?k='+document.getElementById('k').value;"/>

thanks in advance, George

解决方案

You can even use the submit button this way:

 <input type="submit" id="Go" value="Search" onclick="document.location='http://testsearch/results.aspx?k=StackOverflow'; return false;" />

Semantically submit button is used to submit forms not redirect pages. You should use normal button type for this. However as i showed you can use the submit button too but that is not semantic i think.

The below line prevents the form from being submitted.

return false;

That is what you are missing in your code :)

Thanks

这篇关于html按钮v.s. html提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆