[英]When I use input type=“button” a function works well but when I use button it doesn`t
I call the same function with those two types of button. 我用这两种类型的按钮调用相同的功能。 With the first it works perfectly but with button it works well at the first but in less than one second it looks like refresh the page. 使用第一个,它可以完美地工作,但是使用按钮,则可以在第一个中很好地工作,但是不到一秒钟,它看起来就像刷新页面一样。
<input type="button" id="bProv" value="filtrar" onclick="filtroP()"/>
<button id="bProv" onclick="filtroP()">filtrar</button>
The type
of a <button>
element defaults to submit
, so it will run the JS and then immediately submit the form. <button>
元素的type
默认为submit
,因此它将运行JS,然后立即提交表单。
Use <button type="button" ...
if you don't want form submission. 如果您不想提交表单,请使用<button type="button" ...
That said, hard coding a button (which does nothing without JS) goes against the principles of unobtrusive JavaScript (which are part of best practise programming for the WWW). 也就是说,对按钮进行硬编码(没有JS不会执行任何操作)违反了不干扰JavaScript的原理(这是WWW最佳实践编程的一部分)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.