OnBlur 和 OnFocus 事件无法正常工作

发布于 2024-09-06 03:39:18 字数 540 浏览 1 评论 0原文

您好,我正在尝试让我的 onblur 和 onfocus 功能正常工作,但它们似乎没有做正确的事情 - 我试图说如果“全名”字段为空,则将“全名”放入该字段中如果有人输入了某些内容,请保持原样。目前,如果显示“全名”,它会清除该字段,如果该字段为空,则会将“全名”放入其中。但我的问题是,每当有人在其中输入任何内容时,都会在该字段中输入“全名”。

这是我正在使用的代码

函数 doFocusNew() { if ($('.fullname').val() != null && $('.address').val() != 无效的) { $('.fullname').val('') } };

函数 doBlurNew() { if ($('.fullname').val() != null && $('.address').val() != 无效的) { $('.fullname').val('全名') } };

干杯杰米

Hi I'm trying to get my onblur and onfocus functions to work but they don't seem to be doing the right thing - i'm trying to say if the field "fullname" is empty then place "Full Name" in the field and if someone has entered something leave it as it is. At the moment it clears the field if it says "Full Name" and if its empty it puts "Full Name" in. But my problem is that whenever anyone types anything in it keeps putting "Full Name" in the field.

Here's the code i'm using

function doFocusNew() {
if ($('.fullname').val() != null && $('.address').val() !=
null) {
$('.fullname').val('')
}
};

function doBlurNew() {
if ($('.fullname').val() != null && $('.address').val() !=
null) {
$('.fullname').val('Full Name')
}
};

Cheers Jamie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-09-13 03:39:18

.val() 中的值永远不会是 null,它们将是空字符串,因此您需要进行一些调整。我认为这就是您想要的:

function doFocusNew() { 
  if ($('.fullname').val() == 'Full Name') {
    $('.fullname').val(''); //focused, "Full Name" was there, clear it
  } 
}
function doBlurNew() { 
  if ($('.fullname').val() == '') { 
    $('.fullname').val('Full Name'); //blurred, nothing there, put "Full Name" in
  } 
}

如果它们是文本框,这是完整/紧凑的版本:

$(function() {
  $('.fullname').focus(function() {
    if(this.value === 'Full Name') this.value = '';
  }).blur(function() {
    if(this.value === '') this.value = 'Full Name';
  });
});

更新:因为您动态添加它们:

$(function() {
  $('.fullname').live('focus', function() {
    if(this.value === 'Full Name') this.value = '';
  }).live('blur', function() {
    if(this.value === '') this.value = 'Full Name';
  });
});

请务必已修复所有错误,例如,在最新的 jQuery 1.4.2 版本上,此功能可以正常工作,我看到您现在使用的是 1.4.0。

The values from .val() will never be null, they'll be empty strings, so you need a bit of adjustment. I think this is what you're after:

function doFocusNew() { 
  if ($('.fullname').val() == 'Full Name') {
    $('.fullname').val(''); //focused, "Full Name" was there, clear it
  } 
}
function doBlurNew() { 
  if ($('.fullname').val() == '') { 
    $('.fullname').val('Full Name'); //blurred, nothing there, put "Full Name" in
  } 
}

If they're textboxes, here's the complete/compact version:

$(function() {
  $('.fullname').focus(function() {
    if(this.value === 'Full Name') this.value = '';
  }).blur(function() {
    if(this.value === '') this.value = 'Full Name';
  });
});

Update: since you're adding them dynamically:

$(function() {
  $('.fullname').live('focus', function() {
    if(this.value === 'Full Name') this.value = '';
  }).live('blur', function() {
    if(this.value === '') this.value = 'Full Name';
  });
});

Be sure to have all the bug fixes, e.g. on the latest jQuery 1.4.2 release for this to work, I see you're on 1.4.0 right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文