2013-07-15 40 views
5

打开表格时光标正在文本框中等待帮助文本。所以有一些验证错误发生。所以当我打开表单时,我不想重点关注,用户必须点击文本并输入文本。但是在这里没有任何聚焦事件,尽管光标在文本框中。如何从文本框中删除焦点?

if(!$.trim($("#productsTextArea1").val())){ 
     helpTip = "help string ...."; 
     $("#productsTextArea1").val(helpTip); 
     $("#productsTextArea1").css("color","#999"); 
     $("#productsTextArea1").click(function(event){ 
      if($("#productsTextArea1").val().indexOf("Enter a return")>-1){ 
       $("#productsTextArea1").css("color","black").val(""); 
      } 
     }); 
    } else { 
     $("#productsTextArea1").blur(); 
    } 

请指教...

JSFIDDLE

+0

你为什么要运行在页面加载这个功能呢?为什么不推迟,直到用户实际点击textarea? –

回答

9

UPDATE:

使用HTML5占位符属性,你不需要使用任何JS。

<textarea id="productsTextArea1" name="product" rows="5" 
placeholder="Some Hint Text is Placed Here"></textarea> 

要禁用所有输入元素上的自动对焦,请使用此选项。

$(function(){ 
     $('input').blur(); 
    }); 

要确定停用一个特定元素的自动对焦,通过如下元素ID:

$(function(){ 
    $('input#someid').blur(); 
}); 
+0

我已在加载表单中添加,但不禁用? – user2444474

+0

http://jsfiddle.net/AVbJk/ - 在这里检查 – user2444474

+0

实际上没有自动对焦,你在问什么。请问你能改说你的问题吗? – defau1t