Jquery 获取最接近的img标题并设置为字段标题

Jquery 获取最接近的img标题并设置为字段标题,jquery,Jquery,我将用一个例子来解释我的问题: 以下是我的html代码: <td>Name<a href="http://manual"><span><img src="x" width="15" title="The name></span></a></td> <td><input type="text" name="name" maxlength="80" size="93" ></td>

我将用一个例子来解释我的问题:

以下是我的html代码:

<td>Name<a href="http://manual"><span><img src="x" width="15" title="The name></span></a></td>
<td><input type="text" name="name" maxlength="80" size="93" ></td>
但是没有成功。如果有人能帮我解决这个问题,我将不胜感激

谢谢,

您可以这样做:

$('form').find(':input').each(function(){
    if(!$(this).attr('title') && $(this).attr('type') != "hidden" ){
        var title = $(this).closest("tr").find('td').find("img").attr("title");
        $(this).attr('title', title);

    }
});
    $('form').find(':input').each(function(){
       var image = $(this).closest('tr').find('img');
    });
这将搜索最近的tr标记并搜索其中的图像。

您可以执行以下操作:

    $('form').find(':input').each(function(){
       var image = $(this).closest('tr').find('img');
    });
$('form').find(':input').each(function(){
    if(!$(this).attr('title') && $(this).attr('type') != "hidden" ){
        var title = $(this).parents('td').prev().find('img').attr('title');
        $(this).attr('title', title);

    }
});

这将搜索最近的tr标记并搜索其中的图像。

首先,查找所有不带标题的表单输入,它们不会隐藏。然后在当前行中搜索img并复制其标题

$('form').find(':input').each(function(){
    if(!$(this).attr('title') && $(this).attr('type') != "hidden" ){
        var title = $(this).parents('td').prev().find('img').attr('title');
        $(this).attr('title', title);

    }
});
$('form :input:not([title]):not([type="hidden"])').each(function() {
  var title = $(this).closest('tr').find('img').attr('title');
  $(this).attr('title', title);
});

用于测试。

首先,查找所有不带标题的表单输入,这些表单输入不会隐藏。然后在当前行中搜索img并复制其标题

$('form :input:not([title]):not([type="hidden"])').each(function() {
  var title = $(this).closest('tr').find('img').attr('title');
  $(this).attr('title', title);
});

用于测试。

选择器会很方便。至少接受答案至少让我醒来接受答案!!选择器会很方便。至少接受一个答案至少让我醒来接受一个答案!!只有一件事。可能存在这样一种情况,即在某个字段附近没有专门的img。在这种情况下,有没有一种方法可以阻止它拾取最近的img,因为它拾取了,但它不正确@ComeRun如果它找到了一个img,它必须在当前tr中。这个标准是什么,或者你能举一个例子,在哪里发生这种情况?这里有一个很好的例子,你可以看到我们在tr中有两个td。第一个有一个img,需要取标题。但是第二个问题,不需要设置img,因此不需要设置标题。@Comeun由此,我会说Kanishka的答案应该最符合您的要求,因为他寻找前面的td。只有一件事。可能存在这样一种情况,即在某个字段附近没有专门的img。在这种情况下,有没有一种方法可以阻止它拾取最近的img,因为它拾取了,但它不正确@ComeRun如果它找到了一个img,它必须在当前tr中。这个标准是什么,或者你能举一个例子,在哪里发生这种情况?这里有一个很好的例子,你可以看到我们在tr中有两个td。第一个有一个img,需要取标题。但是第二个,没有img,因此标题需要设置。@Comeun由此,我会说Kanishka的答案应该最符合您的要求,因为他寻找前面的td。谢谢Kanishka,这是对我最有用的答案。谢谢Kanishka,这是对我最有用的答案。