duanhe1965 2010-12-28 17:01
浏览 19
已采纳

在输入字段中显示错误/成功消息

I have an input field that asks for an email address followed by a submit button. Here is the code associated with that:

HTML

<div class="email">

    <form action="handler.php" method="post" id="hgb-signup">
    <input type="text" name="email" id="email" value="Enter your email address">
    <input type="submit" name="submit" id="submit" value="&raquo;" />
    </form>

</div>

JAVASCRIPT

Event.observe(window, 'load', function() {
    jQuery('#email').focus(function() {
        jQuery(this).val('');
    });
    jQuery("#hgb-signup").submit(function(e){
        var dataString = jQuery(this).serialize();
        jQuery('#email').val('Sending... please wait...');
        jQuery.getJSON('handler.php?callback=?', {
            "sent_data": dataString }, 
            function(received_data) {
                jQuery('#email').val(received_data.message);
            }
        );
        e.preventDefault();
    });
});

Right now, when the page loads the #email input field reads "Enter your email address". When you click on the submit button, it overwrites the #email value with "Sending... please wait..." and depending on whether there's been a problem or the submission was successful, there will be a respective message inside the #email field indicating such. Now, when I click inside the input field, the "Enter your email address" text disappears .val(''), and when I click outside the field, that message is still gone. I initially had a .blur() that put the message back, which is the desired result, however, when users would enter their email address and hit submit, it would submit "Enter your email address" and not the actual email address, because when you hit the submit button .blur() was called.

Now knowing what the goal is, is there a .blur() alternative and if not, how do I best handle this situation?

Basically, here's what I'm looking for:

The #email field should read "Enter your email address" at any given time when the cursor is not inside the field and should come back when the cursor leaves the field, but I would like the error/success message to stay in there after the form has been submitted, unless the user clicks back inside the field and leaves, then it can say "Enter your email address" again.

Thanks!

  • 写回答

1条回答 默认 最新

  • douchui3933 2010-12-28 17:11
    关注

    Use .blur(), but check the val() first. Replace with "Enter your email" only when it's empty

    jQuery('#email').blur(function() {
        var $this = jQuery(this);
        if ($this.val() == '') $this.val('Enter your email');
    });
    

    Here's a jsfiddle for you, you can check it out. I also added a check to focus() handler so it would not remove contents when you click on an input field after entering your email.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路