显示一个输入并隐藏另一个输入

时间:2013-02-19 23:29:16

标签: jquery

我在页面中有很多输入,我想只显示一个,直到你按回车键。当您按Enter键时,当前输入消失并显示隐藏的输入。

<input type="text" value="Name" />
<input type="text" value="Password" />

这是jQuery代码:

$('input').each(function(){

    $(this).fadeIn();

        $(this).on('keyup', function(e) {
            if (e.which == 13) 
                return true;
            else return false;

        });
    $(this).fadeOut();
});

我制作了这段脚本,但它不起作用。它在同一时间向我显示两个输入,然后消失。

我应该改变什么来获得理想的结果?

1 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/z7ZNL/1/

<input type="text" value="Name" />
<input type="text" value="Password" style="display: none;" />

$('input').on('keyup', function(e) {
  if (e.which == 13) 
    $('input').not(this).fadeIn();
    $(this).fadeOut();
});

这只是处理两个输入的足够代码。你的描述提到“很多”。为此,您需要某种计数器或位置标记来循环遍历所有元素。

我同意popnoodles。这是奇怪的行为。请谨慎使用。