简体   繁体   English

从下一个td中选择隐藏的输入[jQuery]

[英]Select hidden input from within next td [jQuery]

I have a table layed out like this: 我有一张这样布置的桌子:

        <td>
                somename
        </td>
        <td class="hoverable value" >
                somevalue
        </td>
        <td class="changed">

        </td>
        <td class="original value">
            <input type="hidden" value="somevalue" />
        </td>

And what I'm trying to do is, I hover over the hoverable td which turns it into a textbox. 我想做的是,将鼠标悬停在可悬停的td上,它将其变成文本框。 Once I hover out I want to check the hidden field for it's original value and put an image in changed if the 2 are different from each other. 悬停后,我想检查隐藏字段的原始值,并在2彼此不​​同的情况下更改图像。 I already have this: 我已经有这个了:

$(document).ready( function() {
    var newHTML = '';

    $('table td.hoverable').hover(
    function () {
        var oldHTML = $(this).html().trim();
        $(this).html('<input type=\'text\' value=\'' + oldHTML + '\' size=\'' + ((oldHTML).length + 2) +'\' />');
    },
    function() {
        newHTML = $('input', this).val();
        var oldHTML = $(this).next('td.original').children('hidden').val();
       if(newHTML != oldHTML) {
            $(this).next('td.changed').html('Changed');
        }
        $(this).html(newHTML);
    })
});

but it doesn't work. 但这不起作用。 What fails apparently is grabbing the value of the hidden field, and I've tried selecting it in several different ways but just can't get to it. 显然失败的是获取隐藏字段的值,并且我尝试以几种不同的方式选择它,但无法做到这一点。 Any ideas or tips are gratefully appreciated ;) 任何想法或技巧,不胜感激;)

You need to use .nextAll() 您需要使用.nextAll()

var oldHTML = $(this).nextAll('td.original').find('input:hidden').val();

Why? 为什么? Because .next() is taking the next element and seeing if it matches the selector. 因为.next()正在获取下一个元素,并查看它是否与选择器匹配。 If you use .nextAll() you get the next element that matches the selector. 如果使用.nextAll(),则会得到与选择器匹配的下一个元素。

尝试

$(this).next('td.original').find('input:hidden').val();
var oldHTML = $(this).next('td.original').children(':hidden').val();

Replace 更换

var oldHTML = $(this).next('td.original').children('hidden').val();

With

var oldHTML = $(this).next('td.original').find('input:hidden').val();

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

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