onclick registering for multiple buttons with the same class name

时间:2016-02-12 20:33:51

标签: javascript jquery

Right now when I click on a separate button that uses the class "btn-danger" it will get removed. How can I modify my functions to only remove anything when its related to the function being ran?

<!DOCTYPE html>
<html>
   <head>
      <title>Selectize Example</title>
      <link rel="stylesheet" href="Selectize/css/selectize.css">
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
      <script type="text/javascript" src="Selectize/js/standalone/selectize.js" type="text/javascript"></script>
   </head>
   <body>
      <select name="Manufacturer" id="Manufacturer" class="selectize">
          <option value="ALL" selected>All Manufacturers</option>
          <option value="1">Acme Corp.</option>
          <option value="2">Contoso Ltd.</option>
          <option value="3">Electronic, Inc.</option>
      </select>
      <script type="text/javascript">
         $(".selectize").selectize({create: false})
      </script>
   </body>
</html>

Button:

function UpdateTrash(wo)
{
    jQuery.ajax({
        type: "POST",
        url: "functions/markTrash.php",
        data: 'wo='+wo,
        cache: false,
        success: function(response)
        {
            //alert("Record successfully updated");
        }
    });
}

$("#dataTables-example").on('click', '.btn-danger', function () {
    $(this).parent().parent().remove();
});

$("#dataTables-example2").on('click', '.btn-danger', function () {
    $(this).parent().parent().remove();
});

Other button:

<td><br><center><button type="button" name="trashButton1" class="btn btn-danger" onClick="UpdateTrash(<?php echo $orow['WorkOrder']; ?>);"/>Trash</button></a></center></td>

1 个答案:

答案 0 :(得分:0)

I assume the issue is in your jQuery selector.

[alias] pushup = !git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) -> $(this)

<button></button> -> .parent()

Here is a working example. https://jsfiddle.net/gcLt9d8f/

HTML:

<div></div>

JS (jQuery):

<div id="dataTables-example">
<button class='btn-danger'>
Button 1
</button>
</div>

<div id="dataTables-example2">
<button class='btn-danger'>
Button 2
</button>
</div>