简体   繁体   English

当我使用input type =“ button”时,功能很好,但是当我使用button时,它没有

[英]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.

相关问题 当我使用输入type = button时,它的值不会发送到服务器 - When I use input type=button the value of it is not send to the server 有时当我按下按钮时它有时会不起作用 - sometimes when I press the button it works sometimes it doesn't work jQuery文本字段仅接受数字作为输入,这很好用,但是当我使用数字键盘时,它不起作用 - JQuery textfield only accepts numbers as input, this works well but when I use my numpad it does not work 当我点击按钮时功能无法运行 - Function doesn't run when I click on the button 当我将其放入 onclick 时该函数有效,但当我尝试在页面加载 (JavaScript) 中使用时该函数不起作用 - Function works when I put it in an onclick but doesn't work when I try to use in on page-load (JavaScript) 当我使用媒体查询时,HTML单选按钮选中的功能没有显示? - Html Radio Button checked function don't when i use media query? 我正在制作一个打印99瓶歌曲歌词的功能。 当我使用 console.log 时,它可以工作,但是当我使用 return 时它不起作用 - I am making a function to print the lyrics of 99 bottles song. When I use the console.log, it works but it doesn't work when I use return 当我将它与 onClick 中的其他 function 一起使用时,useState 不起作用 - useState doesn't work when I use it with other function in onClick 为什么当我 go 到不同的页面时,幻灯片打开菜单的按钮不起作用,但在主页上它起作用了? - Why doesn't the button for a slide open menu work when i go to a different page but on the main page it works? 无法在我正在制作的计算器应用程序中使用点按钮<input type="number"> - Can't use the dot button in the calculator application I'm making in <input type="number">
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM