开发者

Is this a valid jquery conditional statement?

 if (tag == 'td' && $(this).hasClass('status')) {
        // I am clearing everything inside the td with class status
        $(this).html('')
    }

But it doesn't seem to clear... Any suggestion...

I am customizing a form plugin,

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea' )
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
        开发者_运维知识库    this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
        else if (tag == 'td' && $(this).hasClass('status')) {
            // if you want to remove everything, use this
            $(this).html('');
        }
    });
};


Why not just do:

$('td.status').empty();


Looking at the code (in your edit), and assuming you call the function by doing something like $('#myForm').clearForm() (where myForm is a form element), then its never going to process td elements. The code takes a form & then recurses onto that form's :input elements to clear them. Given that td is not an input, they won't be included in the clearing.

If that is how you're using it, you could customise it as follows to get it to clear your tds (within the form) as well:

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form') {
            $('td.status', this).empty();
            return $(':input', this).clearForm();
        }
        if (type == 'text' || type == 'password' || tag == 'textarea' )
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜