开发者

Check all fields together?

There are three field. i want check each a of field if have value, show class .koko. if on all field not have value, hide class .kok开发者_JAVA百科o.

How is it?

in the this code, each field is check separate but i want check all fields together:

<input type="text" name="ok" class="ko" value="">
<input type="text" name="ok" class="ko" value="">
<input type="text" name="ok" class="ko" value="">
<div class="koko" style="display: none;">Hello, how are you?</div>


$('.ko').live("keyup", function () {
    var $val = $(this).val();
    $('.koko').show();
    if ($val == '') {
        $('.koko').hide()
    }    
});

DEMO


You could do it this way:

$('.ko').live("keyup", function () {
    var $val = $(this).val();
    $('.koko').show();
    var collectiveValue = '';
    $('.ko').each(function(){collectiveValue += this.value})
    if(collectiveValue.length == 0)
        $('.koko').hide();
});

http://jsfiddle.net/abdQc/


$('.ko').live("keyup", function () {
    var showOrHide = $(".ko[value^='']").length >= 0;
    $('.koko').toggle(showOrHide);
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜