2017-06-15 97 views
0

我有一個動態表,它從數據庫中獲取值。我有一個onclcik運行查詢從數據庫中刪除記錄的按鈕。目前而不是刪除記錄(被點擊)它刪除最後一個記錄可用。我從這裏更新我找出問題,當我提供身份證的郵件以某種方式最後的ID被髮送到查詢,是否有無論如何我可以刪除被選中的ID。請查看代碼以獲取更多信息刪除功能刪除表中的最後一條記錄。 js jquery.updated

//this is how im creating dynamic table 
 
//just for the test 
 
function() { 
 
    
 
    }); 
 

 
    });
<table> 
 
    <thead> 
 
    <tr> 
 
     <td> 
 

 
     </td> 
 
    </tr> 
 

 
    </thead> 
 
    <tbody id="table"></tbody> 
 
</table> 
 

 

 
<button type="button" id="chochk" style="display:none" class="sukuti">delete</button>

+1

你在哪裏設置變量'ad'? – Barmar

+0

變量廣告是ID,onclcik按鈕向查詢發送特定的ID,它將從數據庫中刪除具有該ID的行。 – david

+0

但是你從來沒有把它放在任何地方。如果你檢查多行,應該刪除哪一行? – Barmar

回答

0

也許你可能會更好地直接聽取複選框的change事件,然後獲得event.target的值?在你懷疑的代碼中獲得變量ad的方式有些問題,但沒有看到它是如何得出的,很難看出原因。 下面的例子顯示了一個簡單的方法來獲取複選框的值,希望這可能會導致您正確的答案。

$('.test').on('change', function(e) { 
 
    alert(e.target.value); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
Value of 1: <input type="checkbox" class="test" value="1" /><br /> 
 
Value of 2: <input type="checkbox" class="test" value="2" />

0

代替複選框的,使用單選按鈕,所以用戶只能選擇一次一個。然後在刪除按鈕的處理程序中,獲取選中按鈕的值並將其發送到服務器。

//this is how im creating dynamic table 
 
//just for the test 
 
function() { 
 
    var table = document.getElementById('tablebody'); 
 

 
    for (var i = 0; i < (test.length); i = i + 2) { //test is an array of values from db 
 
    var row = table.insertRow(-1); 
 

 
    var cell3 = row.insertCell(-1); 
 
    var cell1 = row.insertCell(-1); 
 
    var cell2 = row.insertCell(-1); 
 

 
    cell1.innerHTML = //some html span 
 
    cell2.innerHTML = //some html span 
 

 
    //this is the checkbox which onclick shows the button 
 
    cell3.innerHTML = '<input type="radio" name="checkbox" class="your" value="ID of current row">'; 
 
    } 
 

 

 

 
    ///this is how i am displaying a button on click and also delete function 
 
    $(document).on('click', ".your", function() { 
 
    $('#chochk').show(); 
 
    }); 
 

 
    $(".sukuti").click(function() { 
 
    var ad = $(".your:checked").val(); 
 
    $.post("test.jsp", { 
 
     id: ad //ad is the ID which will delete the specifIC ID being clicked 
 
    }, function(data) { //sending a query to delete from db working good 
 

 
    }); 
 

 
    });
<table> 
 
    <thead> 
 
    <tr> 
 
     <td> 
 

 
     </td> 
 
    </tr> 
 

 
    </thead> 
 
    <tbody id="table"></tbody> 
 
</table> 
 

 

 
<button type="button" id="chochk" style="display:none" class="sukuti">delete</button>