简体   繁体   English

如何使用jQuery在两个文本框中包含内容的基础上隐藏或显示div?

[英]How can I hide or show a div based on if there is content in two textboxes using jQuery?

I have 2 textboxes on a page and they both have the class "OrderEntry". 我在页面上有2个文本框,它们都具有“ OrderEntry”类。

I want it so if there is any text in either of of these textboxes, it will show the div with ID "OrderPanel". 我想要这样,如果这两个文本框中的任何一个都有文本,它将显示ID为“ OrderPanel”的div。 If the textboxes are cleared, backspaced or empty, it will hide the div "OrderPanel". 如果文本框被清除,后退空格或为空,则将隐藏div“ OrderPanel”。

So, the page loads, "OrderPanel" is hidden and both "OrderEntry" textboxes are empty. 因此,页面加载完毕,“ OrderPanel”被隐藏,两个“ OrderEntry”文本框均为空。 A user types a single character into either one of the textboxes and the "OrderPanel" is shown. 用户在任一文本框中键入一个字符,然后显示“ OrderPanel”。 If BOTH of the "OrderEntry" textboxes are empty, then the div is hidden. 如果“ OrderEntry”文本框都为空,则div被隐藏。

I tried subscribing to the $(".OrderEntry").keydown() event but it seemed to apply both independently. 我尝试订阅$(“。OrderEntry”)。keydown()事件,但它似乎两个都独立应用。 Which means it would still show hide if one was empty and not based on both? 这意味着如果一个为空而不是基于两个都不为空,它将仍然显示隐藏?

Any clues on how to do this in jQuery? 关于如何在jQuery中执行此操作的任何线索?

I'd use keypress I think, but that's just my preference. 我想使用按键,但这只是我的偏爱。

In the keywhatever function, check both textboxes. 在任何键功能中,选中两个文本框。

$( '.OrderEntry' ).keydown(function(){
    var content = false;
    $( 'OrderEntry' ).each(function(){
        if( $( this ).val() != '' )
            content = true;
    });
    if( content )
        $( 'orderPanel' ).show();
    else
        $( 'orderPanel' ).hide();
});

Or something similar 或类似的东西

You're on the right track. 您走在正确的轨道上。 Basically on the key event of choice (keydown, keypress, etc.) rather than showing .OrderPanel immediately, do a check on the contents of the .OrderEntry entities. 基本上是根据所选的关键事件(按键,按键等),而不是立即显示.OrderPanel ,而是要检查.OrderEntry实体的内容。 If they both have values, show the panel, if not, don't. 如果它们都有值,则显示面板,否则,不显示。

EDIT: 编辑:

To add to hookedonwinter's thought - I think the OP wants the panel to only show if all entry fields are completed, so I'd invert the boolean flag in your example to: 补充一下hookedonwinter's想法-我认为OP希望面板仅在所有输入字段都完成时才显示,因此我将示例中的boolean标志反转为:

$( '.OrderEntry' ).keydown(function(){
    var content = true;
    $( 'OrderEntry' ).each(function(){
        if( $( this ).val() == '' )
            content = false;
    });
    if( content )
        $( 'orderPanel' ).show();
    else
        $( 'orderPanel' ).hide();
});

That way, if any of the .OrderEntry fields is empty, .OrderPanel isn't shown.` 这样一来,如果有任何.OrderEntry领域是空的, .OrderPanel不shown.`

I would do something like this: 我会做这样的事情:

$('.OrderEntry').change(function() {
  var bothEmpty = true;
  $('.OrderEntry').each(function() {
    if($(this).text().length > 0) {
      bothEmpty = false;
    }
  });
  if(bothEmpty) {
    $('#OrderPanel').show('slow');
  } else {
    $('#OrderPanel').hide('slow');
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM