Javascript 从表中同一行的输入文本中获取值

Javascript 从表中同一行的输入文本中获取值,javascript,jquery,function,html-table,Javascript,Jquery,Function,Html Table,我有一张多行的桌子。每行包含2个文本输入类型字段和一个按钮,该按钮在单击时运行函数 基本上,所有的输入字段和按钮都具有与附加jQuery相同的id。现在我只想传递与按钮位于同一行的文本字段的值。有人知道我该怎么做吗 $("#teams").find('tbody') .append($('<tr>') .append($('<td>') .text(thisarray.matchId)

我有一张多行的桌子。每行包含2个文本输入类型字段和一个按钮,该按钮在单击时运行函数

基本上,所有的输入字段和按钮都具有与附加jQuery相同的id。现在我只想传递与按钮位于同一行的文本字段的值。有人知道我该怎么做吗

 $("#teams").find('tbody')
        .append($('<tr>')

            .append($('<td>')
                .text(thisarray.matchId)
                .prop('id','matchId')
            )

            .append($('<td>')
                .text(thisarray.homeTeam)
            )
            .append($('<td>')
                .text(thisarray.awayTeam)
            )
            .append($('<td>')
                .append($('<input>')
                    .prop('type', 'text')
                    .prop('id', 'scoreHome')
                )
            )
            .append($('<td>')
                .append($('<input>')
                    .prop('type', 'text')
                    .prop('id', 'scoreAway')
                )
            )
            .append($('<td>')
                .append($('<input type="button" value="Ok" id="sendGoals"/>')
                )
            )
        );

如果它们在同一行中,则来自另一个文件。那么它们一定在DOM中,就像

<tr>
  <td><input type="text" /></td>
  <td><input type="button" /></td>
</tr>
请注意,将有两个家长

td元素

tr元素


然后转到输入类型的子元素并获取其值

不需要id或其他东西。你可以用索引得到它们

$("table tr td input[type=button]").on("click", function(){
  var value1 = $(this).parent().parent().find("input:eq(0)").val();
  var value2 = $(this).parent().parent().find("input:eq(1)").val();
  // do your job with value1 and value2
});

下面是一个小例子:

填充以下内容:

<table id="theTable"></table>
使用此选项:

$(function(){
    var _rows=10;
    var _body="";
    for(i =0;i<_rows;i++){
        _body+="<tr>"+
            "<td><input type='text' class='text1'></input></td>"+
            "<td><input type='text' class='text2'></input></td>"+
            "<td><input type='button' class='theButton' value='Click Here'></input></td>"+
            "</tr>";
    }
    $("#theTable").html(_body);
    $(".theButton").on("click",function(){
        alert("Text1: "+$(".text1",$(this).parent().parent()).val()+", Text2: "+$(".text2",$(this).parent().parent()).val());
    });
 })

你不应该在HTML元素中使用相同的ID。我们不会猜测。!请显示一些代码。请发布您遇到的问题,如果需要,请附带一个或类似的,以演示问题。与附加jquery id的id相同的id必须是唯一的。您应该首先显示您的努力,这正是我需要的!然而,当我尝试在自己的代码中这样做时,它将不起作用。这是我手头的一把小小提琴。我不知道你能不能用它做点什么。您的代码运行得很好,只是忘记了用};,关闭.on函数;,您需要选择JSFIDLE接口左上角的jquery库,这里是FIDLE的一个固定版本:现在就可以使用了。非常感谢你的帮助。我用你的代码重写了我的代码,现在我明白了!再次感谢!很高兴我能帮忙:
<table id="theTable"></table>
$(function(){
    var _rows=10;
    var _body="";
    for(i =0;i<_rows;i++){
        _body+="<tr>"+
            "<td><input type='text' class='text1'></input></td>"+
            "<td><input type='text' class='text2'></input></td>"+
            "<td><input type='button' class='theButton' value='Click Here'></input></td>"+
            "</tr>";
    }
    $("#theTable").html(_body);
    $(".theButton").on("click",function(){
        alert("Text1: "+$(".text1",$(this).parent().parent()).val()+", Text2: "+$(".text2",$(this).parent().parent()).val());
    });
 })