javascript
  • jquery
  • 2016-02-12 76 views 0 likes 
    0

    現在,當我點擊使用類「BTN-危險」它會移除一個單獨的按鈕。如何修改我的函數,以便只在與正在運行的函數相關時刪除任何內容?的onclick註冊多個按鈕具有相同的類名

    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(); 
    }); 
    

    按鈕:

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

    其他按鈕:

    <td><center> 
    <button type="button" name="rush1" class="btn btn-outline btn-danger" onClick="UpdateRush(<?php echo $orow['WorkOrder']; ?>);"/>Rush</button> 
    </center><br><center> 
    <button type="button" name="pool1" class="btn btn-outline btn-info" onClick="UpdatePool(<?php echo $orow['WorkOrder']; ?>);"/>RFP</button></a> 
    </center></td> 
    
    +0

    問題描述不是很清楚。你可以創建一個JsFiddle來演示這個問題嗎? –

    回答

    0

    我認爲這個問題是在你的jQuery選擇。

    $(this) - ><button></button>

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

    這裏是一個工作示例。 https://jsfiddle.net/gcLt9d8f/

    HTML:

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

    JS(jQuery的):

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