JavaScript Repetitive data on click event and submit form with ajax

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the form : input text and input hidden. On click event I fill value in the hidden control. After first submit everything is Ok. But after second click and second submit with the same modal, I have double data in console like this:

Object {isSuccess: true, carId: 13550, message: "Post success"}
Object {isSuccess: true, carId: 13550, message: "Post success"}

I think that the problem is very simple for somebody, but I can't resolve it alone. This code is from HTML DOM:

<table id="carsInventory">
<tr>
    <td>
        <button type="button" data-toggle="modal" data-target="#inv-bid-modal" data-car-id="13551" data-car-brand="MAZDA" data-car-model="Mazda6" class="btn btn-large btn-bids">Bid</button>
    <td>
</tr>
<tr>
    <td>
        <button type="button" data-toggle="modal" data-target="#inv-bid-modal" data-car-id="13550" data-car-brand="CADILLAC" data-car-model="CTS" class="btn btn-large btn-bids">Bid</button>
    <td>
</tr>
</table>


<div class="modal fade" id="inv-bid-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-toggle="bidModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>
            </div>
            <form id="inv-bid-form" method="post">
                <div class="modal-body">
                    <p class="intro-bid">
                        Your Maximum Bid:
                    </p>
                    <div class="input-group">
                        <span class="input-group-addon">$ </span>
                        <input id="userBid" type="text" value="" class="form-control" name="userBid" required pattern="\d+(\.\d{2})?">
                        <span class="input-group-addon">.00</span>
                    </div>
                    <i class="intro-bid">
                        ($100 USD Bid Increments)
                    </i>
                    <input id="invCarId" type="hidden" value="" name="invCarId" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
                    <input type="submit" class="btn btn-bid" value="Bid Now">
                </div>
            </form>
        </div>
    </div>
</div>

Javascript (bottom loading)

$(document.body).on('click', "[data-target=#inv-bid-modal]", function(e)
   {

       var that     = $(this)
       var carBrand = that.attr('data-car-brand'),
           carModel = that.attr('data-car-model'),
           title    = $("#inv-bid-modal .modal-title"),
           car      = that.attr('data-car-id');

       submitBidForm( e, car );

       title.html('');
       title.append(carBrand + ' ' + carModel);

   });

   function submitBidForm(data, id)
   {
       var bidForm      = $("#inv-bid-form"),
           bidModal     = $("#inv-bid-modal"),
           objectBtn    = $(data.target),
           carId        = id,
           bidField     = $('#userBid'),
           carIdField   = $('#invCarId');;

       carIdField.val(carId);

       bidForm.submit(function(event)
       {
           event && event.preventDefault();
           var that     = $(this),
               bid      = bidField.val();

           if( bid < 100 )
           {
               setBsMessages('warning', 'Incorect increment!');
               bidField.val('');
               carIdField.val('');

           }else{

               var request =  $.ajax({
                   url          : '/car/rest/setBid/',
                   method       : 'POST',
                   async        : false,
                   data         : that.serialize(),
                   success      : function(result,textStatus, jqXHR)
                   {

                       if(result.isSuccess)
                       {
                           bidModal.modal('hide').fadeOut('fast');
                           objectBtn.attr('disabled',true);
                           bidField.val('');
                           carIdField.val('');
                       }else{
                           bidModal.modal('hide').fadeOut('fast');
                           bidField.val('');
                           carIdField.val('');
                           setBsMessages('error', 'Invalid bid!');
                       }
                   },
                   error: function (result, textStatus, errorThrown) {
                       bidModal.modal('hide').fadeOut('fast');
                       bidField.val('');
                       carIdField.val('');
                       setBsMessages('error', 'Error! ' + result);

                   }
               });
               return false;
           }
       });
   }

   function setBsMessages(type, message)
   {
       var messages = $("#inv-bid-messages");

       messages.append('<div id="bs-alert" class="alert alert-' + type + ' fade in">' + message+ '</div>');

       $('#bs-alert').delay(2000).fadeOut("slow", function () { messages.remove(); });
   }

PHP

public function setBid() {
    header('Content-type: text/json');
    header('Content-type: application/json');
    $result = false;

    if ( $_POST['invCarId'] ) {

        $this->load->model('bid');
        $bidModel = new bid();

        $carId = (int) $_POST['invCarId'];
        $bidValue = (int) $_POST['userBid'];

        if ( $bidModel->isExistBid($carId))
        {
            $data = [
                'isSuccess' => 'exist',
                'carId'     => $carId,
                'message'   => 'Bid exist'
            ];
            echo json_decode($data);
            exit;
        }

        if ( $bidModel->addBid( $carId, $this->current_user->id, $bidValue ) ) {

            $result = true;
        } else {
            $result = false;
        }

        $data = [
            'isSuccess' => (bool) $result,
            'carId'     => $carId,
            'message'   => 'Post success'
        ];

        echo json_encode($data);
        exit;

    } else {
        echo json_decode($result);
        exit;
    }
}

Thanks in advance.

Answer:1

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

  1. javascript some method
  2. javascript array some method
  3. javascript object some method

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

  1. click event dynamically created button jquery
  2. jquery click dynamically created element
  3. click on dynamically created button
  4. click function on dynamically created element
  5. click event on dynamically created element jquery
  6. jquery bind click dynamically created element
  7. click event for dynamically created button javascript
  8. click function for dynamically created button jquery
  9. click function on dynamically created element jquery
  10. click event for dynamically created button in asp.net c#
  11. click event for dynamically created li in jquery
  12. click event for dynamically created button c#
  13. trigger click on dynamically created element

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...

I need to pass a dynamic param to an applet. This is my controller: 'use strict'; angular.module('jworkApp') .controller('AppletCtrl',['$scope', function (scope) { scope....

I need to pass a dynamic param to an applet. This is my controller: 'use strict'; angular.module('jworkApp') .controller('AppletCtrl',['$scope', function (scope) { scope....