php - modal, button on modal executing many time

538

I am trying to send data on button on modal using jQuery, first time it is working well but when I click second time it execute twice and for third time it execute 3 times, I want to execute that button only once, what is wrong exactly? my modal

`<div id="Modal" role="dialog">
 <div >
  <div >
   <div >
    <button type="button" data-dismiss="modal">&times;</button>
    <h4 >product</h4>
   </div>
   <div >
    <label>Quantity</label>
    <input type="text" id="qty" value="">
    <label>Note:</label>
    <input type="text" id="note" value="">
   </div>
   <div >
    <button id="" type="button" data-dismiss="modal">Add</button>
   </div>
  </div>
 </div>
</div>`

my jQuery :

`

$('#btnbtn').click(function(){
   $('#Modal').modal('show');
    $('#addbutton').click(function(){
    alert();
  });
});`
790

Answer

Solution:

You are addingclick handler multiple time whenever you are clicking on#btnbtn button. No need of attachingclick event handler to#addbutton on each click. Change your code like below:

    $('#btnbtn').click(function(){
       $('#Modal').modal('show');
    });

   $('#addbutton').click(function(){
        alert();
   });
610

Answer

Solution:

Instead of using .click() when binding the button you can use one(). It will only execute once.

$('#btnbtn').click(function(){
   $('#Modal').modal('show');
    $('#addbutton').one('click', function(){
    alert();
  });
});`

People are also looking for solutions to the problem: php - Selecting strings from database that doesn't have questions marks not at the end

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.