简体   繁体   English

JQuery:如何在表内选择包装在 TD 中的特定输入

[英]JQuery: How to select a specific input wrapped in a TD inside a table

I have a table, whose rows and td's are created with a foreach.我有一个表,它的行和 td 是用 foreach 创建的。 So there is no predicting how many rows there might be.因此无法预测可能有多少行。 Each row has two buttons accompanied by an two adjacent input fields.每行有两个按钮,伴随着两个相邻的输入字段。 See image: Table Image When you click a button, I need to grab the value inside it's adjacent input which is wrapped in a td.参见图片:表格图像当您单击一个按钮时,我需要获取它的相邻输入中的值,该输入包含在 td 中。 I have tried everything: prev(), closest(), eg().我什么都试过了:prev()、closest()、eg()。 And I can not figure out how to isolate the specific input on click .而且我无法弄清楚如何在 click 上隔离特定的输入。 The best I have achieved is applying some css to ALL the inputs, but not the specific partnered input.我取得的最好成绩是将一些 css 应用于所有输入,而不是特定的合作输入。

HTML HTML

<tbody>
                @foreach($registeredChildren as $child)
                <tr>
                    <td class="col-2"><a href="'/getchild/'+ {{$child->child_id}}">{{$child->childFirstName}}</a>&nbsp &nbsp {{$child->childLastName}}</td>

                    <!--========TIME IN===========-->
                    <td class="col-3 pb-2 timeIn">
                        <input id="{{$child->child_id}}" class=" form-control " value="{{$child->timeIn}}" style="text-align:center;"  type="text">
                    </td>

                    <td><button class="btn btn-outline-success timeInReset">Reset</button></td>

                    <!-- //========TIME Out ===========//-->
                    <td class="col-3 pb-2"><input style="text-align:center;" class="form-control " type="text" ></td>
                    <td><button id="timeOut/{{$child->child_id}}" class="btn btn-outline-success out">Reset</button></td>
                </tr>
                @endforeach
           </tbody>

Script - This Does Not Work!脚本 - 这不起作用!

$(document).ready(function(){

$("button.timeInReset").click( function () {

 $(this).closest("td.timeIn>input").css({"color": "red", "border": "2px solid red"});

});

closest doesn't look up sibling elements, but from current level up the DOM tree for parents. closest不查找兄弟元素,而是从当前级别向上查找父级的 DOM 树。

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree对于集合中的每个元素,通过测试元素本身并在 DOM 树中向上遍历其祖先,获取与选择器匹配的第一个元素

$("button.timeInReset").click( function () {
    $(this).closest("tr").find("td.timeIn>input").css({"color": "red", "border": "2px solid red"});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM