jQuery 如何从 td 内部获取输入文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2546445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:46:07  来源:igfitidea点击:

How to get input text value from inside td

jquery

提问by Ashish Rajan

<tr>
 <td>
      <input type="text" name="desc[]">
 </td>
 <td>
      <input type="text" name="duration[]">
 </td>
 <td>
      <input type="text" name="start[]" class="start">
 </td>
 <td>
       <input type="text" name="wait[]">
 </td>
 <td>
       <input type="text" name="end[]">
 </td>
 <td>
      <input type="text" name="phone[]">
 </td>
</tr>

I have such multiple rows. I need to fetch the values off all tds on keyup event on the td with input name start(and i also need to avoid values of desc[] and phone[]). Since they are many i cannot go with ids, need help in parent child approach with jquery.

我有这么多行。我需要从输入名称开始的 td 上的 keyup 事件上的所有 tds 中获取值(我还需要避免使用 desc[] 和 phone[] 的值)。由于它们很多,我无法使用 ID,因此在使用 jquery 的父子方法方面需要帮助。

I was able to reach parent tr on keyup event on the above mentioned td but not able to access values of input field in other tds. the jquery code i used

我能够在上述 td 上的 keyup 事件上访问父 tr,但无法访问其他 td 中输入字段的值。我使用的 jquery 代码

$(document).ready(function(){
    $(".start").keyup(function(){
        alert($(this).parent().get(-3).tagName);
    })
});

回答by Felix Kling

Ah I think a understand now. Have a look if this really is what you want:

啊我想现在明白了。看看这是否真的是你想要的:

$(".start").keyup(function(){
    $(this).closest('tr').find("input").each(function() {
        alert(this.value)
    });
});

This will give you all input values of a row.

这将为您提供一行的所有输入值。

Update:
To get the value of notall elements you can use :not():

更新:
要获取并非所有元素的值,您可以使用:not()

$(this).closest('tr').find("input:not([name^=desc][name^=phone])").each(function() {
     alert(this.value)
});

Actually I am not 100% sure whether it works this way, maybe you have to use two nots instead of this one combining both conditions.

实际上我不是 100% 确定它是否以这种方式工作,也许您必须使用两个nots 而不是结合这两个条件的这个。

回答by Andru

var values = {};
$('td input').each(function(){
  values[$(this).attr('name')] = $(this).val();
}

Haven't tested, but that should do it...

还没有测试,但应该这样做......

回答by Pajoc

Maybe this will help.

也许这会有所帮助。

var inputVal = $(this).closest('tr').find("td:eq(x) input").val();

var inputVal = $(this).closest('tr').find("td:eq(x) input").val();

回答by mmacaulay

I'm having a hard time figuring out what exactly you're looking for here, so hope I'm not way off base.

我很难弄清楚您到底要在这里寻找什么,所以希望我没有偏离基础。

I'm assuming what you mean is that when a keyup event occurs on the input with class "start" you want to get the values of all the inputs in neighbouring <td>s:

我假设你的意思是当输入上发生 keyup 事件时,类为“start”,你想获得相邻 <td> s 中所有输入的值:

    $('.start').keyup(function() {
        var otherInputs = $(this).parents('td').siblings().find('input');

        for(var i = 0; i < otherInputs.length; i++) {
            alert($(otherInputs[i]).val());
        }
        return false;
    });