简体   繁体   English

使用 Bootstrap 禁用弹出内容中的按钮侦听器

[英]Disable Button Listener inside a popover content using Bootstrap

I want to submit different input data using the same pre-generated html form template (they just have different variable names as titles).我想使用相同的预生成 html 表单模板提交不同的输入数据(它们只是具有不同的变量名称作为标题)。 On my page, I have two buttons which have basically the same popover effects.在我的页面上,我有两个按钮,它们具有基本相同的弹出框效果。 When I clicked submit (this is the button inside a .popover-content , it appears that I submit both forms even though one of them is hidden.当我单击submit时(这是.popover-content内的按钮,即使其中一个被隐藏,我似乎也提交了 forms 。

function handlePopover (button) {
  const id = button.attr('id');
  $(document).on('click', '#close', function(){
    botton.popover('hide');
  }
  $(document).on('click', '#submit', function(){
    if(id === 'button1') alert("button1");
    else if(id === 'button2') alert("button2");
  }
}

This is how I used this function:这就是我使用这个 function 的方式:

const button1 = $("#button1");
const button2 = $("#button2");
handlePopover(button1);
handlePopover(button2);

It's probably because I added a listener like this $(document).on(...) .这可能是因为我添加了一个像这样的监听$(document).on(...) But I didn't know how to solve this problem.但我不知道如何解决这个问题。 Can I deactivate the listener when the popover content is hidden?隐藏弹出框内容时可以停用侦听器吗? Thank you in advance!先感谢您!

I have figured this out.我已经想通了。 You need to add a listener to the popover button before adding a listener to the submit button.在向提交按钮添加侦听器之前,您需要向弹出按钮添加侦听器。

button.on('shown.bs.popover', function() {
  // add a listener to the submit button 
  $(document).on('click', '#submit', function(){
    if(id === 'button1') alert("button1");
    else if(id === 'button2') alert("button2");
  }
}

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

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