开发者

How can I get jquery to clear a table cell?

My table looks like this:

How can I get jquery to clear a table cell?

How can I get jquery to remove the dropdown or clear the contents from the last cell if my first cell has a value in it? The dropdown should only appear if the first two cells are blank.

<table>
<tr>headings here</tr>
<tr>
 <td>name</td>
 <td>id</td>
 <td>other id</td>
 <td>status</td>
 <td>select location</td>
</tr>
<tr>
 <td>name</td>
 <td>id</td>
 <td>other id</td>
 <td>status</td>
 <td>se开发者_JS百科lect location</td>
</tr>
<tr>
 <td>name</td>
 <td>id</td>
 <td>other id</td>
 <td>status</td>
 <td>select location</td>
</tr>
</table>

EDIT

Got it. I needed a trim().

http://jsfiddle.net/RN5Dn/1/


assuming that your table looks like

<table>
    <tr>
        <td>soon it will fade away</td>
        <td></td>
    </tr>
    <tr>
        <td>Last Samurai</td>
    </tr>
</table>

you can do this

$(function(){
    if(!$("table tr:first").find("td:first").text())
    {
        $("table tr:last").delay('1000').fadeOut("slow");
    }
});

you can remove the dropdown by selecting it and then removing it by using remove like

$("#idOfYourDropDown").remove();

here is the working fiddle http://jsfiddle.net/3nigma/uF7M5/4/

in the fiddle i have used delay so that the value remains visible for a bit of time

jquery :first

jquery :last

EDIT

i am assuming that if there is no name you want to hide/remove the select location

$(function(){

$("table tr").each(function(){

    if(!$("td:first",this).text())
    {
        $("td:last",this).delay("1000").fadeOut("slow");
    }

});

});

here is the fiddle http://jsfiddle.net/3nigma/uF7M5/3/


Quick and dirty http://jsfiddle.net/nTExe/

$("tr:gt(0)").each(function() {
    if ($(this).find("td:eq(0)").html().length > 0 && $(this).find("td:eq(1)").html().length > 0) {
        $(this).find("td:last").html("");
    }
});


Just hide it with a CSS property or something, then show it again when necessary. If you want, you can manage a hidden input value that indicates whether the dropdown is visible or not.


Just put an id to your table (e.g. id="tbl") and then:

$(document).ready(function() {
    $('#tbl tr').each(function(i) {
        if (i === 0) return; // skip header row
        var children = $(this).children();
        var tdVal1 = children.eq(0).text();
        var tdVal2 = children.eq(1).text();
        if (tdVal1 || tdVal2) {
            children.eq(-1).html('');
        }
    });
});

You can test it here: http://jsfiddle.net/protron/8AfVN/3/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜