1

I am going to explain my question by an example:

Here is my html code:

<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>

What I want to do is get the closest img title to my input field, and set it as the field title. The purpose is basically check all the page's form fields and set the closest image title to their title, if the title has not been set.

What I have done so far:

$('form').find(':input').each(function(){
    if(!$(this).attr('title') && $(this).attr('type') != "hidden" ){

        var title = $(this).siblings('td').find("img").attr("title");
        $(this).attr('title', title);

    }
});

But no success. I'd be grateful if someone can help me out with this.

Thanks,

4

5 回答 5

1
$('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);

    }
});
于 2013-01-15T07:02:50.083 回答
1

你可以这样做:

    $('form').find(':input').each(function(){
       var image = $(this).closest('tr').find('img');
    });

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

于 2013-01-15T07:07:44.353 回答
1
$('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);

    }
});
于 2013-01-15T07:07:55.787 回答
1
$('form').find(':input').each(function(){
    if(!$(this).attr('title') && $(this).attr('type') != "hidden" ){
        var title = $(this).closest("tr").find("img").attr("title");
        $(this).attr('title', title);   
    }
});
于 2013-01-15T07:09:10.463 回答
1

First, find all form inputs without title, which aren't hidden. Then search the img in the current row and copy it's title

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

JSFiddle for testing.

于 2013-01-15T07:41:37.547 回答