简体   繁体   English

Bootstrap onClick 按钮事件

[英]Bootstrap onClick button event

This seems a silly question but just got bootstrap and it doesn't gives any examples on the website about adding a Javascript callback to a button...这似乎是一个愚蠢的问题,但刚刚获得引导程序,它没有在网站上提供任何关于向按钮添加 Javascript 回调的示例......

Tried setting my button an id flag and then尝试将我的按钮设置为 id 标志,然后

<div class="btn-group">
  <button id="rectButton" class="btn">Rectangle</button>
  <button class="btn">Circle</button>
  <button class="btn">Triangle</button>
  <button class="btn">Line</button>
  <button class="btn">Curve</button>
  <button class="btn">Pic</button>
  <button class="btn">Text</button>
  <button class="btn">Gradient</button>
</div>

I want to add a callback to Rectangle button...我想向矩形按钮添加回调...

$('#rectButton').on('show', function (e) {
    //ACTION
})

cant get the point of bootstrap callbacks.无法获得引导回调的重点。

All I could found on the web is oriented to Rails + Bootstrap... no bootstrap and JS only.我在网上能找到的所有内容都是面向 Rails + Bootstrap 的……没有引导程序和 JS。

There is no show event in js - you need to bind your button either to the click event: js 中没有show事件 - 您需要将按钮绑定到click事件:

$('#id').on('click', function (e) {

     //your awesome code here

})

Mind that if your button is inside a form , you may prefer to bind the whole form to the submit event.请注意,如果您的按钮位于form ,您可能更愿意将整个表单绑定到submit事件。

If, like me, you had dynamically created buttons on your page, the如果像我一样在页面上动态创建按钮,

$("#your-bs-button's-id").on("click", function(event) {
                       or
$(".your-bs-button's-class").on("click", function(event) {

methods won't work because they only work on current elements (not future elements).方法将不起作用,因为它们仅适用于当前元素(而非未来元素)。 Instead you need to reference a parent item that existed at the initial loading of the web page.相反,您需要引用在网页初始加载时存在的父项。

$(document).on("click", "#your-bs-button's-id", function(event) {
                       or more generally
$("#pre-existing-element-id").on("click", ".your-bs-button's-class", function(event) {

There are many other references to this issue on stack overflow here and here .此处此处的堆栈溢出中还有许多其他关于此问题的参考。

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

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