2009-12-21 49 views
0

我正在构建我的新主题,我有一个问题。我使用jQuery显示输入的标签后输入:WordPress的内联标签(jQuery)

this.label2value = function(){ 

    var inactive = "inactive"; 
    var active = "active"; 
    var focused = "focused"; 

    $("label").each(function(){ 
     obj = document.getElementById($(this).attr("for")); 
     if(($(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea")){ 
      $(obj).addClass(inactive); 
      var text = $(this).text(); 
      $(this).css("display","none"); 
      $(obj).val(text); 
      $(obj).focus(function(){ 
       $(this).addClass(focused); 
       $(this).removeClass(inactive); 
       $(this).removeClass(active); 
       if($(this).val() == text) $(this).val(""); 
      }); 
      $(obj).blur(function(){ 
       $(this).removeClass(focused); 
       if($(this).val() == "") { 
        $(this).val(text); 
        $(this).addClass(inactive); 
       } else { 
        $(this).addClass(active); 
       }; 
      }); 
     }; 
    }); 
}; 
$(document).ready(function(){ 
    label2value(); 
}); 

但是,问题是,当我提交通讯,如果不是一个网站的网址WP使用标签作为一个URL(http://YourWebsiteURL)。

我该如何解决这个问题?

回答

0

你的代码用它们的标签填充输入,所以它们最终被提交给服务器。干净的解决方案是停止使用输入值,使用CSS来移动输入下方的标签(相同的位置,较低的Z-index),并使用jQuery在输入上添加/删除透明背景取决于它们是否处于焦点和/或空闲状态。

这样,您就可以通过输入查看标签,如果它是空的,而不是根据焦点

的快速和肮脏的解决办法,因为现有的代码,是设置表单的提交事件,使任何包含自己标签文本的字段在提交表单时都会被清除。

+0

就像透明BG的想法。谢谢。 – Axiol 2009-12-21 23:27:13

0

为什么重新发明轮子,虽然有很多插件可以做到这一点,labelify。它可以使用该字段的原始标签作为字段中显示的文本。只需检查插件页面的例子。