JAVASCRIPT / JQUERY获得第一行的价值

时间:2015-06-16 20:03:43

标签: javascript jquery html

当我点击每个删除按钮时,我正试图获取html表中行的第一个值...

该表从SQL数据库填充......

下面的图片!

我只会提供必要的代码:

enter image description here

HTML按钮:

 Sub FindDuplicatesInColumn()
'Declaring the lastRow variable as Long to store the last row value in the Column1
    Dim lastRow As Long

'matchFoundIndex is to store the match index values of the given value
    Dim matchFoundIndex As Long

'iCntr is to loop through all the records in the column using For loop
    Dim iCntr As Long

'test the column A and insert a column or clear data
    If Range("A1").Value = "PDBC_PFX" Then
        Range("A1").EntireColumn.Insert
        Range("A1").Value = "DUPE_CHECK"
    Else
        Range("A2:A65000").Clear
    End If

'Finding the last row in the Column B
    lastRow = Range("B65000").End(xlUp).Row

'looping through the column B
    For iCntr = 1 To lastRow
        'checking if the cell is having any item, skipping if it is blank.
        If Cells(iCntr, 2) <> "" Then
            'getting match index number for the value of the cell
            matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 2), Range("B1:B" & lastRow), 0)
            'if the match index is not equals to current row number, then it is a duplicate value
            If iCntr <> matchFoundIndex Then
                'Printing the label in the column A
                Cells(iCntr, 1) = "Duplicate"
            End If
        End If
    Next

'auto fit column A
    Columns("A").AutoFit

End Sub

Javascript代码:

<button type="button" class="btn btn-danger btn_delete" id="btn_delete" onclick="delete_function()">Delete</button>

如果我点击任何按钮,我总会收到相同结果的警告:

114

如何获得与单击按钮相同行中存在的相同ID号值?我接受Javascript和Jquery建议

4 个答案:

答案 0 :(得分:2)

将您的代码更改为:

<button type="button" class="btn btn-danger btn_delete" id="btn_delete" onclick="delete_function(this)">Delete</button>

使用first-child

<script type="text/javascript">
    function delete_function(obj) {
        var id = $(obj).closest('tr').find('td:first-child').text();
        alert(id);      
    }
</script>

有一点需要提及:你不应该在html中使用onclick:

$(".btn_delete").on('click',function (){
  var id = $(this).closest('tr').find('td:first-child').text();
  alert(id); 

});

答案 1 :(得分:1)

$('.btn_delete').click(function(){
    var id = $(this).parents('tr').find('td:first-child').text();
    alert(id);
}

onClick总是一个坏主意。

jQuery.click() vs onClick

Why is using onClick() in HTML a bad practice?

答案 2 :(得分:0)

您可以通过多种方式进行此操作,但有一种方法是直接在按钮上设置id,这样您就不必遍历DOM来获取它

<button class="delete" data-id="114">Delete</button>

然后,在您的JavaScript中

$('button.delete').on('click', function( e ) {
    console.log($(this).data('id'));
});

答案 3 :(得分:0)

你总是得到相同的结果,因为你总是得到相同的元素。要获取与您按下的按钮相关的值,您需要在找到元素时使用该按钮。

当您使用jQuery时,您也可以使用它来绑定事件。从HTML

中删除<button type="button" class="btn btn-danger btn_delete" id="btn_delete">Delete</button> 属性
this

在事件处理程序中,您使用单击的按钮($(document).ready(function(){ $('#clients .btn_delete').click(function() { var id = $(this).closest('tr').find('td:first').text(); alert(id); } }); )来查找同一行中的第一个单元格:

printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
                                            ^